0
我已經繼承了這段代碼,並且已經玩了很長時間了。我現在是第一次通過倒計時工作,但在第一次會話刷新後,我相信我還有第二次,也許第三次計時器正在運行。我相信我需要實現clearInterval(),以保持這一堅實的20分鐘計時器,但我不知道如何正確執行此操作。任何幫助,將不勝感激。什麼是最好的方式來控制這些計時器?
<script type="text/javascript">
var DoLogout = 1;
var WarnMills;
var TimeoutMills;
var WarnDurationMills;
var RedirectURL;
var _timeLeft;
function StartTimeout(TimeoutValue, WarnValue, WarnDuration, URLValue) {
TimeoutMills = TimeoutValue;
WarnMills = WarnValue;
WarnDurationMills = WarnDuration;
RedirectURL = URLValue;
setTimeout(UserTimeout, TimeoutMills);
setTimeout(WarnTimeout, WarnMills);
}
function UserTimeout() {
if (DoLogout == 1) {
top.location.href = RedirectURL;
}
else {
DoLogout = 1;
setTimeout(UserTimeout, TimeoutMills);
setTimeout(WarnTimeout, WarnMills);
}
}
function WarnTimeout() {
_timeLeft = (WarnDurationMills/1000);
updateCountDown();
$find('mdlSessionTimeout').show();
}
function updateCountDown() {
var min = Math.floor(_timeLeft/60);
var sec = _timeLeft % 60;
if (sec < 10)
sec = "0" + sec;
document.getElementById("CountDownHolder").innerHTML = min + ":" + sec;
if (_timeLeft > 0) {
_timeLeft--;
setTimeout(updateCountDown, 1000);
}
else {
window.location.replace("AdminLogin.aspx");
}
if (_timeLeft < (5 * 60)) {
$find('mdlSessionTimeout').show();
}
}
function PreserveSession() {
{
$.ajax({
type: "POST",
url: "SessionKeepAlive.asmx/Check",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: checkAuthenticatedOk,
error: checkAuthenticatedError
});
}
}
function checkAuthenticatedOk() {
DoLogout = 0;
_timeLeft = (20 * 60);
TimeoutMills = (20 * 60 * 1000);
$find('mdlSessionTimeout').hide();
}
function checkAuthenticatedError() {
window.location.replace("AdminLogin.aspx");
}
您可以將'timer ids'推入數組並在必要時檢索它們,或者通過遍歷數組來清除所有計時器。 – Mouser