Knockout setTimeout延迟

时间:2015-07-27 14:17:19

标签: javascript knockout.js

当用户登录我的网站时,我会存储一个"登录到"我的服务器上该用户的参数,每次与服务器交互时都会更新。因此,如果它们在X秒内处于非活动状态,则应将其注销,并刷新其客户端上的页面以显示登录屏幕。一切都运作良好,除了功能以1秒的间隔自我调用,而不是我正在寻找的1700+秒间隔。当每个呼叫都到达服务器时,我不希望这种情况比预期更频繁地运行。

此功能包含在我的knockout viewmodel中。 logoutTimer被声明为全局变量。观察控制台日志我可以看到延迟显示正确的时间,但它似乎每1秒运行一次。

self.autoLoginCheck = function() {
    clearTimeout(logoutTimer);
    //If not, refresh the main screen => that'll push them to the login screen automatically
    myurl = "/api/checkLoginStatus.php";
    var parameters = {
                        'userPublicID':self.userInfo().userPublicID
                    }
    $.post(myurl, parameters, function(data) {
        var getResponse = JSON.parse(data);
        if (getResponse.loggedIn < 1 ) {
            //This is the log out condition
            location.reload(true);
        } else {
            //Check how many more seconds the current log in is good for, that's when we'll check it again
            var msdelay = getResponse.nextCheck * 1000;
            console.log("Delay is " + msdelay);
            logoutTimer = setTimeout(self.autoLoginCheck(), msdelay);
        }
    });
}

我知道这类问题已被多次询问,但是阅读所有这些答案我不知道为什么这个功能每秒都在运行而不是等待30分钟。感谢无论谁给我看明显!

1 个答案:

答案 0 :(得分:1)

setTimeout的第一个参数应该是一个函数,而不是函数的结果。

logoutTimer = setTimeout(self.autoLoginCheck, msdelay);
相关问题