1

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>
3
  • 3
    for (;;) { will block forever. Use setInterval instead. Commented May 21, 2018 at 9:34
  • setTimeout(showBit(), 2000); is equivalent to var tmp = showBit(); setTimeout(tmp, 2000);. Since showBit() returns undefined, your setTimeout call is invalid (you're passing it undefined instead of a function). Commented May 21, 2018 at 9:39
  • Thanks. I used CertainPerformance's response and have it all working! :) Commented May 21, 2018 at 9:44

3 Answers 3

2

A few things went wrong:

 setTimeout(showBit(), 1000);

must be:

setTimeout(showBit, 100);

as you want to pass a function and not execute it immeadiately. Another thing is that you cannot just do

 for(;;) { /*...*/ }

as that blocks the browser forever. You would have to do it asynchronously:

  const delay = ms => new Promise(res => setTimeout(res, ms));

 (async function() {
   while(true) {
      await delay(1000);
      showBit();
   }
 })();

Or a bit simpler with a pseudorecursive timeout:

(function next() {
   showBit();
   setTimeout(next, 1000);
 })();

Or if you dont want to do that manually, just use setInterval:

 setInterval(showBit, 1000);
Sign up to request clarification or add additional context in comments.

2 Comments

I had missed the first point. I was aware of the send (asynchronous running) but your code makes it clearer; thank you.
Glad to help :)
0

Like @CertainPerformance said, setInterval should be used instead. Here are a few good examples of how it can be used - https://www.w3schools.com/jsref/met_win_setinterval.asp

2 Comments

This answer is "in another castle". A visitor that finds this thread should be able to understand your answer without visiting external sites (that might be offline then). Additionaly please don't recommend w3schools, IMO they are oversimplifying things, i would rather recommend MDN.
That's your opinion. I did mention the person giving the correct answer. W3schools is a great reference if you just need to lookup something small. I do agree that MDN is great. But here, w3schools explains the use well.
0
Use **setInterval()** with **Math.round** and **Math.random**? var time = setInterval(f, 2000); function f() { document.getElementById("bit").innerHTML = Math.round(Math.random()); }

Use if

var time = setInterval(function() {
    if (document.getElementById("bit").innerHTML == 0) {
        document.getElementById("bit").innerHTML = 1;
    }
    else {
        document.getElementById("bit").innerHTML = 0;
    }
}, 2000);

2 Comments

Since this is only to run as 1 then 0, random would not make sense...yet! This is perfect for the next step, however. Thank you.
Math.random takes no arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.