javascript JS setInterval 只执行一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7746505/
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
JS setInterval executes only once
提问by MichaelS
I have the following JS functions:
我有以下 JS 函数:
function checkIfGameAlreadyStarted(){
$.get("IsGameAlreadyStarted",null,function(gameAlreadyStarted){
if (gameAlreadyStarted == "true"){
window.location = "index.jsp?content=game";
} else{
alert("bla");
}
});
}
function joinGame(playerForm){
$.get("GenerateClientID",null,function(clientID){
$.get("JoinGame",{
"NAME" : playerForm.elements[0].value,
"ID" : clientID
}
,function(gameParam){
$("#waitingContainer").append("You have joined the game!<br\>Waiting for game creator to start game..");
setInterval(checkIfGameAlreadyStarted(), 1000);
});
});
}
Why does setInterval
executes checkIfGameAlreadyStarted
only once, and not every second?
为什么只setInterval
执行checkIfGameAlreadyStarted
一次,而不是每秒执行一次?
回答by jfriend00
You are passing the result of executing the function instead of the function itself. Since the result of the function is undefined, you are executing checkIfGameAlreadyStarted and then passing undefined to setInterval which doesn't do anything.
您正在传递执行函数的结果而不是函数本身。由于函数的结果未定义,您正在执行 checkIfGameAlreadyStarted,然后将 undefined 传递给 setInterval,它什么也不做。
Instead of this:
取而代之的是:
setInterval(checkIfGameAlreadyStarted(), 1000);
Your statement should be this:
你的陈述应该是这样的:
setInterval(checkIfGameAlreadyStarted, 1000);
without the parentheses at the end of the function name.
函数名末尾没有括号。
When you pass checkIfGameAlreadyStarted()
that calls the function immediately and gets it's return value. When you pass checkIfGameAlreadyStarted
that passes a reference to the function so setInterval can call it later (which is what you want).
当您传递checkIfGameAlreadyStarted()
它时,立即调用该函数并获取它的返回值。当您传递checkIfGameAlreadyStarted
该函数的引用时, setInterval 可以稍后调用它(这就是您想要的)。
回答by Akintunde
To use checkIfGameAlreadyStarted
without parameters, use the method below:
要不checkIfGameAlreadyStarted
带参数使用,请使用以下方法:
setInterval(checkIfGameAlreadyStarted, 1000);
In case checkIfGameAlreadyStarted
has some parameters to be passed, use the method below:
如果checkIfGameAlreadyStarted
有一些参数需要传递,请使用以下方法:
setInterval(function(){checkIfGameAlreadyStarted(a,b);},1000)
This is the best approach I have seen. Other well tested methods are welcomed ,though.
这是我见过的最好的方法。不过,欢迎其他经过良好测试的方法。
Edit
编辑
Passing parameters after the timeout as suggested in the comments is cool but using the above method I stated helps to combat the bind this
problem in case checkIfGameAlreadyStarted()
is a class method like this this.checkIfGameAlreadyStarted()
.
按照评论中的建议在超时后传递参数很酷,但使用我所说的上述方法有助于解决bind this
问题,以防万一checkIfGameAlreadyStarted()
是这样的类方法this.checkIfGameAlreadyStarted()
。
In case you want to pass parameters after timeout, here is how it works,
如果您想在超时后传递参数,这是它的工作原理,
setInterval(checkIfGameAlreadyStarted, 1000, parameter1, parameter2);