Using setTimeout(check, 1000);
calls the function only once. That's not what you are looking for.
What you're looking for is setInterval
which executes a function every n miliseconds.
Look at the below example which waits for the value to be 1, using setInterval
, and then clearing the setInterval
instance once it does.
Wait 4 seconds when running the snippet below:
// First - set the value to 0
myvalue = 0;
// This variable will hold the setInterval's instance, so we can clear it later on
var interval;
function check() {
if (myvalue == 1) {
alert("Value Is Set");
// We don't need to interval the check function anymore,
// clearInterval will stop its periodical execution.
clearInterval(interval);
}
}
// Create an instance of the check function interval
interval = setInterval(check, 1000);
// Update the value to 1 after 4 seconds
setTimeout(function() { myvalue = 1 }, 4000);