我有一个应用程序需要在 28 分钟后超时。它将显示 jQuery 对话框 2 分钟。如果用户在两分钟内单击“确定”,计时器将刷新到 28 分钟,当倒计时达到 0 分钟时,它应该再次显示 jQuery 对话框。
问题是当我调试时,我看到超时变量没有清除超时。单击“确定”后,计时器重置为 28 分钟,但当倒计时达到 0 分钟时,setTimeout 不会再次显示对话框。
这是我的代码:
var timeout;
function timer() {
countDownDate = 0;
console.log("Hello");
countDownDate = 0;
var timeExpires = new Date().getTime();
countDownDate = timeExpires + 1680000;
now = 0;
distance = 0;
if(timeout){
clearTimeout(timeout);
}
// Update the count down every 1 second
var x = setInterval(function () {
// Get todays date and time
now = new Date().getTime();
// Find the distance between now an the count down date
distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (distance < 0) {
if ($("#alert").length) {
var title;
if ($("#alert span").length) {
title = $("#alert span").text();
}
$("#alert div").dialog({
title: title,
modal: true,
buttons: {
Ok: function() {
var foo = $(this);
clearTimeout(timeout);
timer();
foo.dialog('close');
},
Cancel: function() {
esriId.destroyCredentials();
window.location.replace(redirect_uri);
}
}/*,
open: function() {
var foo = $(this);
timeout = setTimeout(function() {
foo.dialog('close');
esriId.destroyCredentials();
window.location.replace(redirect_uri);
}, 120000);
},*/
});
}
timeout = setTimeout($("#alert div").dialog("open"), 120000);
clearInterval(x);
}
}, 1000);
};
这是警报的 HTML div:
<div id="alert" style="display:none">
<span>You will be signed out</span>
<div>You will be signed out due to inactivity on the page. If you wish to stay on the page, please press 'Ok'</div>
</div>