防止onbeforeunload函数暂停javascript计时器 [英] Prevent onbeforeunload function from pausing javascript timer

查看:70
本文介绍了防止onbeforeunload函数暂停javascript计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页上,我有一个倒数计时器,使用javascript的 setTimeout()

In my webpage, I have a countdown timer using javascript's setTimeout().

    function Tick() {
        if (RemainingSeconds <= 0) {
            alert("Time is up.");
            return;
        }
        RemainingSeconds -= 1;
        ElapsedSeconds += 1;
        UpdateTimerDisplay();
        window.setTimeout("Tick()", 1000);
    }

我还在 onbeforeunload上触发了一个功能阻止用户离开页面。

I also have a function triggered on onbeforeunload to "prevent" the user from leaving the page.

    window.onbeforeunload = function () {
        if (!isIEAjaxRequest) {
            return "You should use the logout button to leave this page!";
        }
        else {
            isIEAjaxRequest = false;
        }
    };

问题在于,你确定要离开这个页面吗?窗口提示,它暂停 setTimeout()函数。有关如何防止这种情况的任何想法?

The problem is that when the "Are you sure you want to leave this page?" window prompts, it pauses the setTimeout() function. Any thoughts on how to prevent this?

推荐答案

可能的解决方法可能是使用 var before = new Date()用于存储对话框出现之前的时间,然后使用该日期计算对话框消失后的传递时间,以弥补错过的秒数。

A possible workaround would maybe be to use var before = new Date() to store the time before your dialog appears and then use that one to calculate the passed time after your dialog disappears to make up for the missed seconds.

这篇关于防止onbeforeunload函数暂停javascript计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆