GettingBeforeUnload与setTimeout一起使用 [英] Getting onBeforeUnload to work with a setTimeout
问题描述
我正在尝试使onbeforeunload与各种设置的计时器一起工作,但是当返回到位时,我似乎无法启动它.我希望它可以与计时器一起使用,但是由于某些原因计时器无法正常工作.这是我正在使用的代码,非常感谢您在查看代码时所提供的帮助.
I am trying to get onbeforeunload to work with a set timer of sorts but I can't seem to get it to fire up when the return is in place. I am hoping it would work with a timer but for some reason the timer isn't working. Here is the code I am working and thank you for your help in looking at it much appreciated.
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();
});
推荐答案
onBeforeUnload
与 setTimeout
不兼容.执行 onBeforeUnload
后,将触发 onUnload
,页面将发生变化.这是在调用 setTimeout
回调之前发生的.
onBeforeUnload
doesn't work with setTimeout
. After onBeforeUnload
executes, onUnload
will be triggered and the page will change. This happens before the setTimeout
callback is called.
这篇关于GettingBeforeUnload与setTimeout一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!