var until = $("#time").html();
function updateTime() {
$("#time").html(
date("d", until) + " day(s)<br />" +
date("h", until) + " hour(s)<br />" +
date("i", until) + " minute(s)<br />" +
date("s", until) + " second(s)"
);
}
setInterval("updateTime(until)",1000);
Everytime I run this, I get this error:
Uncaught ReferenceError: until is not defined (anonymous function)
I can't see whats wrong. I've tried to google a lot, but every page that I find, says that setInterval()
is right.
Why are setTimeout and setInterval not accurate? To answer this question, you need to understand that there is a mechanism called event loop in the JavaScript host environment (browser or Node. js). It is necessary for front-end developers to understand this mechanism.
Nested setTimeout calls are a more flexible alternative to setInterval , allowing us to set the time between executions more precisely. Zero delay scheduling with setTimeout(func, 0) (the same as setTimeout(func) ) is used to schedule the call “as soon as possible, but after the current script is complete”.
Method 1: Calling the function once before executing setInterval: The function can simply be invoked once before using the setInterval function. This will execute the function once immediately and then the setInterval() function can be set with the required callback.
To reset the setInterval timer with JavaScript, we can use the clearInterval function. const myFn = () => { console. log("idle"); }; let myTimer = setInterval(myFn, 4000); //... clearInterval(myTimer); myTimer = setInterval(myFn, 4000);
Closures:
setInterval(function() {updateTime(until); }, 1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With