确保特定的 setInterval 函数实例仅运行一次
我正在开发一个 div(父级),它有另外两个 div(菜单和内容),如下所示:
<div id="parent">
<div id="menu"></div>
<div id="content"></div>
</div>
内容 div 中加载的内容是一个 html 文件,它具有一些 javascript 函数,例如重新加载其内容的自动刷新每 5 秒一次。
$(document).ready(function () {
setInterval(function () {
grid.reloadDefaultContent(); //this reloads the content on content div.
}, 5000);
}
页面上有一些链接将不同的内容加载到内容 div 中。到目前为止一切顺利,直到我回到具有自动刷新功能的“主页”。问题是自动刷新从未停止,现在我再次单击返回主页,该函数运行了两次(或者我更改页面并返回主页的次数),从而开启了会话。 我正在使用 jQuery $.load()
加载页面。
关于如何使 setInterval 实例仅运行一次有什么想法吗?
编辑:读完我自己的问题后,我发现它有点不清楚。我的主要问题是,当我回到家时,我的 setInterval 的第一个实例已经在运行,第二个实例开始运行,使得 5 秒的自动刷新速度变得更快,并且每次我都会变得越来越快更改页面并返回主页。这不是我想要的行为。我需要的是当我更改 div 上的内容时停止 setInterval 的实例,并在我返回主页时重新启动它。
I'm working on a div (parent) that has two other divs (menu and content) as follow:
<div id="parent">
<div id="menu"></div>
<div id="content"></div>
</div>
The content loaded in content div is an html file that has a few javascript functions, such as an auto-refresh that reloads its content every 5 seconds.
$(document).ready(function () {
setInterval(function () {
grid.reloadDefaultContent(); //this reloads the content on content div.
}, 5000);
}
There are some links on the page that load differents content into the content div. So far so good, until I go back to "home," which has the auto-refresh function. The problem is that the auto-refresh never stopped, and now that I clicked to go to home again, the function is running twice (or how many times I change the page and come back to home), putting session upon.
I'm loading pages using jQuery $.load()
.
Any ideas on how I can make that instance of setInterval runs only once?
Edit: after reading my own question I saw that it was a bit unclear. My main problem is that when I go back to home, my first instance of the setInterval is already running, and a second starts, making the auto-refresh that was 5 seconds go faster, and it'll go faster and faster every time I change the page and go back to home. This is not the behavior I want. What I need is to STOP the instance of setInterval when I change the content on the div, and restart it when I go back to home.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该可以做到:
它只会启动间隔计时器的一个实例。
This should do it:
It will only initiate one instance of the interval timer.
如果您只希望它运行一次,为什么不直接使用“setTimeout()”呢?尝试一下:
另一种选择是始终重新启动计时器:
哪一种更好取决于您的情况。
If you only want it to run once, why not just use "setTimeout()" instead?Try this:
Another alternative would be to always re-start the timer instead:
Which one is better depends on your circumstances.
如果您希望代码只运行一次,则应该使用 setTimeout() 而不是
setInterval()
。If you want your code to only run once, you should use setTimeout() not
setInterval()
.用户 jQuery 的
.one()
函数User jQuery's
.one()
function在不更改执行 setInterval 的源页面的情况下,您唯一的选择似乎是破解 grid.reloadDefaultContent 函数以使其仅运行一次。如果不了解
grid
是如何定义的,就不可能提供任何建议。Without changing the source page that does the
setInterval
, your only option seems to be to hack thegrid.reloadDefaultContent
function to make it run only once. It is impossible to provide any recommendations without seeing howgrid
is defined.