I've been trying to create a JavaScript function which loops fast through all the 24bit of the rgb-values and displays them in form of a dynamiclly changing div-background
In order to slow the process down enough to see something I was thinking of using the setTimeout method. I did some research on using setTimeout in loops and found this.
setTimeout in for-loop does not print consecutive values
Seems to be describing my problem perfectly. I tried to use the solutions given there. But for some reason it does not work.
Edit: It works now (It uses setInterval, because that makes probably more sense here)
<script>
function setColor(i) {
var c = i.toString(16);
switch(c.length) {
case 1:
c = "00000" + c;
break;
case 2:
c = "0000" + c;
break;
case 3:
c = "000" + c;
break;
case 4:
c = "00" + c;
break;
case 5:
c = "0" + c;
break;
default:
c;
}
document.getElementById("colorContainer").style.background = "#" + c;
}
function loopThroughColors() {
var i = 0x000000;
setInterval(function(intervalId) {
setColor(i);
i += 1;
if (i >= 0xffffff) {
clearInterval(intervalId);
}
}, 100);
}
</script>