Bubblegumz Bubblegumz - 3 months ago 11
Javascript Question

Is it possible to delay a variable in a for-loop with set timeout?

Is it possible to delay the variable

value
inside
window.setTimeout
and have the milliseconds value of the timeout be my
time
variable below?

var value = null;
var time = 5000;

for(var i = 0; i < 30; i++){


if(value === "a"){
console.log("You made it!");
} else {
console.log("You didn't make it!");
}

time = time - 100;

}

Answer

You can also use

for(i=0;i<30;i++)
    setTimeout(function(){ 

    alert('hello');
    }, 5000);  

alert('hey');
}

But it'll work in different way. It'll give "hello" right away and after 5 seconds it'll show "hey" .

You can use "clearTimeout()" method to clear timeout .