简体   繁体   English

JS setInterval 只执行一次

[英]JS setInterval executes only once

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 ,而不是每秒执行一次?

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. 当你传递checkIfGameAlreadyStarted()立即调用该函数并获得它的返回值。 When you pass checkIfGameAlreadyStarted that passes a reference to the function so setInterval can call it later (which is what you want). 当你传递checkIfGameAlreadyStarted并传递对函数的引用时,setInterval可以稍后调用它(这就是你想要的)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM