Mobilpadde
Mobilpadde

Reputation: 1881

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.

Upvotes: 8

Views: 19153

Answers (7)

LoukasPap
LoukasPap

Reputation: 1381

You can also write it as a lambda expression (with JS arrow function) like this:

setInterval(() => updateTime, 1000);

More information here.

Upvotes: 2

Gaddy Evans
Gaddy Evans

Reputation: 71

If you call a function with parameters inside setInterval(), you must add parameters as arguments to the setInterval() function.

Look: setInterval() - Web APIs | MDN

In your case, try this:

setInterval('updateTime', 1000, until);

11 years old question, but may it helps others

Upvotes: 3

scottheckel
scottheckel

Reputation: 9244

SetInterval has global scope, so I'm guessing that until wasn't defined in global scope. Changing your code to the following two will work. I believe this is what you're trying to do.

function updateTime(){
    var until = $("#time").html();
    $("#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()", 1000);

or

function updateTime(until){
     $("#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(function() { updateTime($("#time").html()) }, 1000);

Note: this is assuming that until changes every second; otherwise, I'm not sure why you would have an interval.

Upvotes: 0

jfriend00
jfriend00

Reputation: 707148

The logical explanation would be that the variable until is not a global variable. If it's not global (or captured in a relevant function closure), then it won't still exist when the setInterval fires and tries to evaluate the string you passed as the function call. It's also not a good practice to pass a string to setInterval, you should pass an actual javascript function.

As to how you should change your code, that depends upon whether you want the value of until to be updated each time the interval function is called or you want to capture it just once before it ever runs and use that value for all subsequent invocations of the timer interval. Your code is a bit ambiguous for which way you want that to work (capturing it into a variable once, but then trying to pass it in each time). If you don't want the value of until to ever be updated, you could do it like this:

var until = $("#time").html();

function updateTime(when) {
    $("#time").html(
        date("d", when) + " day(s)<br />" +
        date("h", when) + " hour(s)<br />" +
        date("i", when) + " minute(s)<br />" +
        date("s", when) + " second(s)"
    );
}

setInterval(function() {updateTime(until);}, 1000);

This will capture the scope of until in a function closure and make it available to the anonymous interval callback function. Putting it in a string like you were doing before would not create such a closure.

I also made the calling of updateTime(until) match the declaration of your updateTime() function.

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237817

The trouble is that you're passing the code to setInterval as a string. This means that it's evaluated in the global scope. The variable until does not exist in the global scope, only in the scope where it's defined.

If you pass a function in, this means that the variable until is available (it's "closed in"):

setInterval(function() {
    updateTime(until);
},1000);

Upvotes: 6

Lime
Lime

Reputation: 13524

You are using a form of eval. Use:

setInterval(updateTime,1000);

Upvotes: 0

Gabriel Ross
Gabriel Ross

Reputation: 5198

Closures:

setInterval(function() {updateTime(until); }, 1000);

Upvotes: 21

Related Questions