onfocus/onblur 进度

发布于 2024-11-16 06:13:55 字数 953 浏览 4 评论 0原文

你好,我是 javascript 的新手,今天努力工作,但找不到解决方案。 请帮我。

<script>
    if (/*@cc_on!@*/false) {
        document.onfocusin = startB;
        document.onfocusout = stopB;
    } else {
        window.onfocus = startB;
        window.onblur = stopB;
    }

var x = 1000;
var y = 1;

function startB() {
    if (x !== 'ok') {
        x = x - y;
        document.form.num.value = x;
        timeoutID=setTimeout("startB()", 1000);
    } 
    if (x == 0) {
        x = 'ok';
        document.form.num.value = x;
    }
}

function stopB() {
clearTimeout(timeoutID);
}
</script>
<body onLoad="startB()">
<form name="form">
<input name="num" size="10" readonly="readonly" type="text"> 
</form>

当我打开此页面时,它通常以 1/秒的值倒计时,当我更改窗口/选项卡时,它会停止,我再次回来时它会继续 1/秒,这就是我想要的,但我有一个大问题 -->当我转到此链接并非常快速地更改窗口/选项卡时,它仍然处于卸载状态,它开始在 onblur 状态下倒计时,当我回到 onfocus 状态时,该函数会被读取两次并开始倒计时 1/0.5 秒。 .. 双倍的。

谁能帮我解决这个问题吗? 提前致谢。

Hello I'm newbie in javascript and worked hard today on this and couldn't find the solution.
Please help me.

<script>
    if (/*@cc_on!@*/false) {
        document.onfocusin = startB;
        document.onfocusout = stopB;
    } else {
        window.onfocus = startB;
        window.onblur = stopB;
    }

var x = 1000;
var y = 1;

function startB() {
    if (x !== 'ok') {
        x = x - y;
        document.form.num.value = x;
        timeoutID=setTimeout("startB()", 1000);
    } 
    if (x == 0) {
        x = 'ok';
        document.form.num.value = x;
    }
}

function stopB() {
clearTimeout(timeoutID);
}
</script>
<body onLoad="startB()">
<form name="form">
<input name="num" size="10" readonly="readonly" type="text"> 
</form>

When I open this page it counts down with value 1/sec normally and when I change window/tab it stops, I again come back it's continues 1/sec, this is what I want, but I have a big problem --> When I go to this link and change window/tab very quickly, that is still unloaded, it starts counting down in onblur status, and when I come back to onfocus status, the function is read twice and starts counting down 1/0.5sec... double.

Can anyone help me with this?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左岸枫 2024-11-23 06:13:55

在设置超时之前让 startB 调用 stopB。将 stopB 更改为:

  if (timeoutID) clearTimeout(timeoutID);

这样,如果 timeoutID 已被清除(或尚未设置),则不会抛出错误。

编辑

如果您在加载事件发生之前离开页面,则计时器将在页面获得焦点的情况下启动。如果您在设置超时之前取消它(即 startB 总是 在调用 setTimeout 之前调用 stopB),则不能运行两个计时器。

Have startB call stopB before it sets the timeout. Change stopB to:

  if (timeoutID) clearTimeout(timeoutID);

so that if the timeoutID has already been cleared (or not set yet) it won't throw an error.

Edit

If you navigate away from the page before the load event occurs, then the timer will start without the page getting focus. If you cancel the timeout before setting it (i.e. startB always calls stopB before calling setTimeout), you can't have two timers running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文