javascript setInterval 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7013644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:42:05  来源:igfitidea点击:

setInterval doesn't work?

javascriptjquery

提问by Mobilpadde

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)

未捕获的 ReferenceError:until 未定义(匿名函数)

I can't see whats wrong. I've tried to google a lot, but every page that I find, says that setInterval()is right.

我看不出有什么问题。我试过用谷歌搜索了很多,但我找到的每一页都说setInterval()是对的。

回答by Gabriel Ross

Closures:

关闭:

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

回答by lonesomeday

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

问题是您将代码setInterval作为字符串传递给。这意味着它是在全局范围内评估的。该变量until不存在于全局范围内,仅存在于定义它的范围内。

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

如果您传入一个函数,这意味着该变量until可用(它是“封闭的”):

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

回答by L.Papadopoulos

You can also write it as a lambda expression like this:

你也可以把它写成这样的 lambda 表达式:

setInterval(() => updateTime, 1000);

setInterval(() => updateTime, 1000);

回答by jfriend00

The logical explanation would be that the variable untilis 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.

合乎逻辑的解释是该变量until不是全局变量。如果它不是全局的(或在相关的函数闭包中捕获),那么当 setInterval 触发并尝试评估您作为函数调用传递的字符串时,它不会仍然存在。将字符串传递给 setInterval 也不是一个好习惯,您应该传递一个实际的 javascript 函数。

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

这将捕获until函数闭包中的作用域,并使其可用于匿名间隔回调函数。像以前那样将它放在字符串中不会创建这样的闭包。

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

我还调用了updateTime(until)matchupdateTime()函数的声明。

回答by scottheckel

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.

SetInterval 具有全局范围,所以我猜直到没有在全局范围内定义。将您的代码更改为以下两个将起作用。我相信这就是你想要做的。

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.

注意:这是假设直到每秒都在变化;否则,我不确定你为什么会有间隔。

回答by Lime

You are using a form of eval. Use:

您正在使用一种形式的 eval。利用:

setInterval(updateTime,1000);