功能活动超时不起作用

时间:2014-09-24 00:38:46

标签: javascript timeout

这是我的活动超时功能,但不起作用。我需要帮助才能知道原因,谢谢!

首先,该功能会询问用户他/她是否在这里:

function activityTimeout(){
        //$("#jquery_jplayer").jPlayer("pause");

        clearTimeout(activityTO);

        blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton">&nbsp;or&nbsp;<input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);

        popupTO = setTimeout(abandonCorrection, 60000);
    }

如果用户在这里,则他/她重新开始工作并且该功能重置超时倒计时:

function renewActivityTimeoutUnblock(){
        $.unblockUI();

        renewActivityTimeout();
    }

    function renewActivityTimeout(){
        clearTimeout(activityTO);
        clearTimeout(popupTO);

        activityTO = setTimeout(activityTimeout, 1800000);
    }

2 个答案:

答案 0 :(得分:0)

你不能在同一个函数中clearTimeoutsetTimeout(对于相同的超时),因为函数被调用时执行一次。

function renewActivityTimeoutUnblock(){
    $.unblockUI();

    renewActivityTimeout();
}

function stopTimeout(){
    clearTimeout(activityTO);
}

function renewActivityTimeout(){
    clearTimeout(popupTO);

    activityTO = setTimeout(activityTimeout, 1800000);
}

答案 1 :(得分:0)

也许你没有用,因为你的javascript控制台中可能有这个错误Uncaught ReferenceError: popupTO is not defined

确保声明存储超时的变量。

function activityTimeout(){
    window.clearTimeout(activityTO);

    // make sure that this is an existing function in you js code
    blockInfoMsg('Are you here?.<br>(Automatic quit in <span id="spanActTO">60</span>seconds)<br><br><input type="button" value="Im here!" onclick="javascript:renewActivityTimeoutUnblock()" class="inputButton">&nbsp;or&nbsp;<input type="button" value="Quit Now!" onclick="javascript:abandonCorrection()" class="inputButton">', 0);

    // make sure abandonCorrection is an existing function, and this will timeout for 1 minute
    popupTO = window.setTimeout(abandonCorrection, 60000);
}

function renewActivityTimeoutUnblock(){
    $.unblockUI();
    renewActivityTimeout();
}
var activityTo;  // declare this variable like this
var popupTo;   // declare this variable like this

function renewActivityTimeout(){
    window.clearTimeout(activityTO);
    window.clearTimeout(popupTO);

    // take note of your time here it's 1,800,000 ms that's about 30 minutes
    activityTO = window.setTimeout(activityTimeout, 1800000);
}

记下你的时间,activityTimeout将在30分钟后开始。尝试降低它以便你可以看到。然后使用警报或控制台日志进行自己的调试。

以下是 timeout working 的简短演示,它会在console.log和警告弹出之前延迟3秒。希望这会对你有所帮助。

注意: 不要将setTimeout与setInterval混淆。

setTimeout 只是一个延迟,只执行一次。的 Read more about it here

另一方面,

setInterval就像一个循环,它会在你设置的每个时间间隔内继续调用函数 Read more about it here

要详细了解窗口计时器 just go here