You don't really need to write a function to convert a number to hexadecimal. Number.toString will do that for you.
The disco script should be placed within an anonymous function so the global namespace isn't polluted.
Functions are first class data. Rather than having two branches in disco that are almost exactly the same, assign a function to a variable and use it outside the branches.
Variable names should be descriptive. If you want a minified script, pass it through a minifier when publishing, but not before. The original source should be unminified so it can be read, updated & debugged.
Arrays are indexed starting with 0, not 1 in JS. It's time to start counting like a programmer. Better still, use object properties.
Magic numbers should be replaced with symbolic "constants" (JS doesn't have constants, so a variable will have to do).
Code:
(function () {
var state_incr=2, state_decr=1, state_nochange=0;
var r=255,g=255,b=255,
counter=0,state={}, /* 'state' replaces 'd' */
defaultPeriod=50, discoInterval;
function first_step(name, value) {
...
}
function step(name,value) {
if ((state[name]==state_incr && value<255) || value==0) {
++value;
state[name]=state_incr;
} else if ((state[name]==state_decr && value>0) || value==255) {
--value;
state[name]=state_decr;
}
return value;
}
function unstick() {
if (state.r + state.g + state.b == state_nochange) {
counter=0;
}
}
function disco() {
var delta, done, color;
if (counter==0) {
delta=first_step;
done=function() {}
counter=50;
} else {
delta=step;
done=unstick;
counter--;
}
r=delta('r', r);
g=delta('g', g);
b=delta('b', b);
done();
color = (r << 16 | g << 8 | b).toString(16);
document.body.style.background="#"+color;
}
window.startDisco = function(period) {
if (!period) {
period = defaultPeriod;
}
if (! discoInterval) {
discoInterval = setInterval(disco, period);
}
};
window.stopDisco = function() {
clearInterval(discoInterval);
discoInterval=0;
}
startDisco();
})();
Of coures, the best improvement is simply to discard the whole script. From a graphic design perspective, it's a terrible idea.
Note [code] tags make code more readable. For PHP and HTML, there's [php] and [html] tags, respectively.