启动和停止 JavaScript 刷新 [英] Start and Stop a javascript refresh

查看:56
本文介绍了启动和停止 JavaScript 刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面需要每 60 秒刷新一次.在此页面上,我使用 iBox 来弹出各种项目.我的问题是元刷新会杀死不需要的弹出窗口.请记住,我对 javascript 的经验从零到很少,所以我的解决方案可能从根本上是错误的.

我想出的解决方案是使用 javascript 进行刷新.当页面加载时,我将启动计时器,当 ibox 弹出时,我将清除计时器,当 ibox 关闭时,我将再次启动计时器.

我正在使用一个简单的函数进行设置.

<前>函数 timedRefresh(timeoutPeriod){无功重置Id = 0;resetId=setTimeout("location.reload(true);",timeoutPeriod);}

然后我调用函数.

我的问题源于当我尝试调用 clearTimeout(resetID) 时.我试图从 ibox 脚本的隐藏函数调用该方法,但它实际上并没有清除计时器.我认为这可能是一个范围问题,我可能需要做某种Object.clearTimeout(Object.resetID) 但这只是一个猜测.

解决方案

这样做:

function timedRefresh(timeoutPeriod){window.resetId = 0;//通过前缀window"来明确它是全局的.window.resetId=setTimeout("location.reload(true);",timeoutPeriod);}

然后从相关的 ibox 函数中使用 window.resetId.

<小时>

看到你的评论我来补充一下.

"window." 在浏览器中编写脚本时会起作用,如果您在其他地方使用 JS,它可能不起作用.

不过,只要您在网页上,window 就是全局对象,而 window" 前缀是 IMO 表达清楚的好方法某些变量是全局的;如果你一直使用它,所有没有窗口"的变量.在他们面前的总是本地人.

然而,您应该知道,如果您只使用 resetId 而不带任何前缀且不带 var,它也可以工作,因为任何未用 var 自动限定为 window.

这个简短的指南将教你大部分需要的东西了解 Javascript 中的变量可见性、执行上下文和闭包.它会让你成为一个致命的 Javascript 忍者.

I have a page that needs to be refreshed every 60 seconds. On this page I am using iBox to popup various items. My problem is that a meta refresh kills the popup which is not wanted. Please keep in mind I have zero to little experience with javascript so my solution may be fundamentally wrong.

The solution I came up with is to use javascript to do the refresh. When the page loads I will start the timer, when an ibox pops up I will clear the timer, when the ibox closes I will start the timer again.

I am setting this up by using a simple function.

function timedRefresh(timeoutPeriod){
    var resetId = 0;
    resetId=setTimeout("location.reload(true);",timeoutPeriod);
}

Then I call the function <body onload="timedRefresh(60000)">.

My problem is stemming from when I try to call clearTimeout(resetID). I am trying to call the method from the ibox script's hide function, but it doesnt actually clear out the timer. I think it may be a scope issue and I may need to do some sort of Object.clearTimeout(Object.resetID) but that is just a guess.

解决方案

Do this:

function timedRefresh(timeoutPeriod){
    window.resetId = 0; // make it clear it's global by prefixing "window."
    window.resetId=setTimeout("location.reload(true);",timeoutPeriod);
}

Then from the relevant ibox function use window.resetId.


Seeing your comment I'll add something.

"window." will work when scripting within a browser, should you use JS somewhere else it probably won't work.

As long as you're on a webpage, however, window is the global object and the "window." prefix is IMO a good way to make it clear that certain variables are global; if you use it consistently, all the variables that don't have "window." in front of them are always local.

You should know, however, that it would also work if you simply used resetId without any prefix and without var because any variable that isn't declared with var is automatically scoped to window.

This short-ish guide will teach you most of what you need to know about variable visibility in Javascript, execution contexts and closures. It will send you on your way to become a deadly Javascript ninja.

这篇关于启动和停止 JavaScript 刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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