Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval doesn't work?

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.

like image 782
Mobilpadde Avatar asked Aug 10 '11 15:08

Mobilpadde


People also ask

Why is setInterval not accurate?

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.

What can I use instead of setInterval?

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

Does setInterval run immediately?

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.

How do I reset setInterval?

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);


1 Answers

Closures:

setInterval(function() {updateTime(until); }, 1000);
like image 136
Gabriel Ross Avatar answered Nov 10 '22 14:11

Gabriel Ross