Javascript setInterval 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8779845/
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
Javascript setInterval not working
提问by mauzilla
I need to run a javascript function each 10 seconds.
我需要每 10 秒运行一次 javascript 函数。
I understand the syntax must work like follow but I am not getting any success:
我知道语法必须像下面一样工作,但我没有得到任何成功:
function funcName() {
alert("test");
}
var func = funcName();
var run = setInterval("func",10000)
But this isn't working. Any help?
但这不起作用。有什么帮助吗?
回答by Reid
A lot of other answers are focusing on a pattern that does work, but their explanations aren't really very thorough as to why your current code doesn't work.
许多其他答案都集中在一种有效的模式上,但他们对为什么您当前的代码不起作用的解释并不是很彻底。
Your code, for reference:
您的代码,供参考:
function funcName() {
alert("test");
}
var func = funcName();
var run = setInterval("func",10000)
Let's break this up into chunks. Your function funcName
is fine. Note that when you call funcName
(in other words, you run it) you will be alerting "test"
. But notice that funcName()
-- the parentheses mean to "call" or "run" the function -- doesn't actually return a value. When a function doesn't have a return value, it defaults to a value known as undefined
.
让我们把它分成几块。你的功能没问题funcName
。请注意,当您调用funcName
(换句话说,您运行它)时,您将发出警报"test"
。但请注意funcName()
——括号的意思是“调用”或“运行”函数——实际上并没有返回值。当函数没有返回值时,它默认为一个称为 的值undefined
。
When you call a function, you append its argument list to the end in parentheses. When you don't have any arguments to pass the function, you just add empty parentheses, like funcName()
. But when you want to refer to the function itself, and not call it, you don't need the parentheses because the parentheses indicate to run it.
调用函数时,将其参数列表附加到括号中的末尾。当您没有任何参数来传递函数时,您只需添加空括号,例如funcName()
. 但是当你想引用函数本身而不是调用它时,你不需要括号,因为括号表明要运行它。
So, when you say:
所以,当你说:
var func = funcName();
You are actually declaring a variable func
that has a value of funcName()
. But notice the parentheses. funcName()
is actually the return value of funcName
. As I said above, since funcName
doesn't actually return any value, it defaults to undefined
. So, in other words, your variable func
actually will have the value undefined
.
您实际上是在声明func
一个值为 的变量funcName()
。但请注意括号。funcName()
实际上是 的返回值funcName
。正如我上面所说,由于funcName
实际上不返回任何值,因此默认为undefined
. 因此,换句话说,您的变量func
实际上将具有 value undefined
。
Then you have this line:
然后你有这一行:
var run = setInterval("func",10000)
The function setInterval
takes two arguments. The first is the function to be ran every so often, and the second is the number of milliseconds between each time the function is ran.
该函数setInterval
接受两个参数。第一个是每隔一段时间运行的函数,第二个是每次运行函数之间的毫秒数。
However, the first argument really should be a function, not a string. If it is a string, then the JavaScript engine will use eval
on that string instead. So, in other words, your setInterval is running the following JavaScript code:
然而,第一个参数确实应该是一个函数,而不是一个字符串。如果它是一个字符串,那么 JavaScript 引擎将eval
在该字符串上使用。因此,换句话说,您的 setInterval 正在运行以下 JavaScript 代码:
func
// 10 seconds later....
func
// and so on
However, func
is just a variable (with the value undefined
, but that's sort of irrelevant). So every ten seconds, the JS engine evaluates the variable func
and returns undefined
. But this doesn't really do anything. I mean, it technically is being evaluated every 10 seconds, but you're not going to see any effects from that.
但是,func
只是一个变量(带有 value undefined
,但这有点无关紧要)。所以每隔 10 秒,JS 引擎就会评估变量func
并返回undefined
. 但这真的没有任何作用。我的意思是,从技术上讲,它每 10 秒进行一次评估,但您不会看到任何影响。
The solution is to give setInterval
a function to run instead of a string. So, in this case:
解决方案是提供setInterval
一个函数来运行而不是一个字符串。所以,在这种情况下:
var run = setInterval(funcName, 10000);
Notice that I didn't give it func
. This is because func
is nota function in your code; it's the value undefined
, because you assigned it funcName()
. Like I said above, funcName()
will call the function funcName
and return the return value of the function. Since funcName
doesn't return anything, this defaults to undefined
. I know I've said that several times now, but it really is a very important concept: when you see funcName()
, you should think "the return value of funcName
". When you want to refer to a function itself, like a separate entity, you should leave off the parentheses so you don't call it: funcName
.
请注意,我没有给它func
。这是因为func
它不是您代码中的函数;这是价值undefined
,因为你分配了它funcName()
。就像我上面说的,funcName()
会调用函数funcName
并返回函数的返回值。由于funcName
不返回任何内容,因此默认为undefined
. 我知道我已经说过好几次了,但这确实是一个非常重要的概念:当您看到 时funcName()
,您应该想到“的返回值funcName
”。当你想引用一个函数本身时,就像一个单独的实体,你应该去掉括号,这样你就不会称它为:funcName
。
So, another solution for your code would be:
因此,您的代码的另一个解决方案是:
var func = funcName;
var run = setInterval(func, 10000);
However, that's a bit redundant: why use func
instead of funcName
?
但是,这有点多余:为什么要使用func
而不是funcName
?
Or you can stay as true as possible to the original code by modifying two bits:
或者您可以通过修改两个位来尽可能保持原始代码的真实性:
var func = funcName;
var run = setInterval("func()", 10000);
In this case, the JS engine will evaluate func()
every ten seconds. In other words, it will alert "test"
every ten seconds. However, as the famous phrase goes, eval
is evil, so you should try to avoid it whenever possible.
在这种情况下,JS 引擎将func()
每十秒评估一次。换句话说,它会"test"
每十秒发出一次警报。然而,正如那句名言所说,eval
是邪恶的,所以你应该尽可能避免它。
Another twist on this code is to use an anonymous function. In other words, a function that doesn't have a name -- you just drop it in the code because you don't care what it's called.
这段代码的另一个转折是使用匿名函数。换句话说,一个没有名字的函数——你只要把它放在代码中,因为你不关心它叫什么。
setInterval(function () {
alert("test");
}, 10000);
In this case, since I don't care what the function is called, I just leave a generic, unnamed (anonymous) function there.
在这种情况下,因为我不关心函数被调用,我只是在那里留下一个通用的、未命名的(匿名)函数。
回答by techfoobar
Change setInterval("func",10000)
to either setInterval(funcName, 10000)
or setInterval("funcName()",10000)
. The former is the recommended method.
更改setInterval("func",10000)
为setInterval(funcName, 10000)
或setInterval("funcName()",10000)
。前者是推荐的方法。
回答by qwertymk
Try this:
尝试这个:
function funcName() {
alert("test");
}
var run = setInterval(funcName, 10000)
回答by ThiefMaster
That's because you should pass a function, not a string:
那是因为你应该传递一个函数,而不是一个字符串:
function funcName() {
alert("test");
}
setInterval(funcName, 10000);
Your code has two problems:
您的代码有两个问题:
var func = funcName();
calls the function immediately and assigns the return value.- Just
"func"
is invalid even if you use the bad and deprecated eval-like syntax of setInterval. It would besetInterval("func()", 10000)
to call the function eval-like.
var func = funcName();
立即调用函数并分配返回值。"func"
即使您使用 setInterval 的错误且已弃用的类似 eval 的语法,Just也是无效的。这将是setInterval("func()", 10000)
调用类似 eval 的函数。