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?
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...