The following code is intended to display a 0, then a few seconds later change this to 1, then back to 0, and so on, ad infinitum.
The problem is this (obviously) freezes the webpage itself. I could use an animated gif but want to expand this further and code becomes the better choice.
How might I do this?
<div id="bit" style="width: 100px; height: 100px; margin: auto; border: 1px solid #000; text-align: center;"></div>
<script>
var bitValue = 0;
for (;;) {
setTimeout(showBit(), 2000);
}
function showBit() {
document.getElementById("bit").innerHTML = bitValue;
bitValue = Math.abs(bitValue - 1);
}
</script>
for (;;) {will block forever. UsesetIntervalinstead.setTimeout(showBit(), 2000);is equivalent tovar tmp = showBit(); setTimeout(tmp, 2000);. SinceshowBit()returnsundefined, yoursetTimeoutcall is invalid (you're passing itundefinedinstead of a function).