让 onBeforeUnload 使用 setTimeout

Gil*_*t V 1 javascript onbeforeunload settimeout

我试图让 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();
});
Run Code Online (Sandbox Code Playgroud)

Joh*_*lak 6

onBeforeUnload不适用于setTimeout. 后onBeforeUnload执行,onUnload将被触发,该网页将改变。这发生在setTimeout调用回调之前。