【发布时间】:2012-09-24 19:37:06
【问题描述】:
我正在尝试让 onbeforeunload 使用某种设置的计时器,但当返回到位时,我似乎无法启动它。我希望它可以与计时器一起使用,但由于某种原因计时器无法正常工作。这是我正在工作的代码,非常感谢您帮助查看它。
var validNavigation = false;
function wireUpEvents() {
var leave_message = 'Leaving the page';
jQuery(
function goodbye() {
jQuery(window).bind('onbeforeunload', function() {
setTimeout(function() {
setTimeout(function() {
jQuery(document.body).css('background-color', 'red');
}, 10000);
},1);
return leave_message;
});
});
function leave() {
if(!validNavigation) {
killSession();
}
}
//set event handlers for the onbeforeunload and onunloan events
window.onbeforeunload = goodbye;
window.onunload=leave;
}
// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function () {
wireUpEvents();
});
【问题讨论】:
-
请在发布问题时正确格式化您的代码,这样更容易理解它在说什么。它还可以帮助您自己,任何体面的代码编辑器都将具有
indent或format功能,可以为您完成工作。对于 SO 使用空格而不是制表符,并在每行前加上 4 个空格以使代码块显示为代码。 -
抱歉,我发布代码总是很复杂,别以为我会安静地得到它。
标签: javascript settimeout onbeforeunload