2

I'm new to programming, so please don't be harsh with my skills. Anyways, here is my code, I'm attempting to make a simple countdown loop.

var number = 30;
var countdown = true;

while(countdown === true) {
    subtract();
    if(number === 0) {
        countdown = false;
    }
}

function subtract() {
    setTimeout(function() {
        console.log(number);
        number = number - 1;
    }, 1000);
}

What did I do wrong?

2
  • 2
    setTimeout() queues a function up to be executed later but doesn't pause execution of the current function. So none of your timeouts could occur until after the while loop finishes - which it never will since it is testing a value set in the timeouts... Commented Mar 29, 2014 at 1:21
  • take a look at your cpu usage while running this code ;) your while loop is a resource hog. nothing else gets a look in Commented Mar 29, 2014 at 1:25

4 Answers 4

2

Javascript has function-level block execution. One function runs to completion before another function is given ability to execute. Your while loop is maintaining the execution baton, so the other functions in setTimeout are never given a chance.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you as well, along with everyone else who gave me advice with setTimeout()
I'd upvote answers, but I cannot yet. This is my first time using Stackoverflow with an account.
What's the threshold, 10 rep? I think you should have that now.
1

although javascript is called asynchonous in this case it does not call the subtract function until the first one is finished. (details http://ejohn.org/blog/how-javascript-timers-work/)

this should work

var number = 30;

setTimeout(function() {
    console.log(number);
    number = number - 1;
}, 1000);

1 Comment

This does not iterate 30 times. See the answer from @Malk for how to use setInterval instead.
1

setInterval is the function to set a function to run periodically. https://developer.mozilla.org/en-US/docs/Web/API/Window.setInterval

var number = 30;
var countdown = true;
var timer;

function update(){
   console.log(number);
   number = number - 1;
   if (number === 0)
      clearInterval(timer);
}


timer = setInterval(update, 1000);

3 Comments

Thanks for the fix! Could you or someone explain to me what timer = setInterval(update, 1000); means? Like, a small rundown on how it works? I can tell what each piece means, but I'd really like to grasp what it really means.
Check the link I posted. It's all laid out and demonstrated.
Yeah, I just noticed the link. Thanks!
0

When using the setTimeout function you actualy call a function after X miliseconds (in this case X=1000, which is 1 second).

'While' is the function you want to do the countdown with, right? So in your subtract function just write:

console.log(number);
number = number - 1;

You can also drop the subtract function and just write:

while(countdown === true) {
    console.log(number);
    number = number - 1;
    if(number === 0) {
        countdown = false;
    }
}

Or drop the while function:

function subtract() {
    id(countdown === true)
    setTimeout(function() {
        console.log(number);
        number = number - 1;
        subtract();
    }, 1000);
    else countdown = false;
}
subtract();

Comments

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.