[英]How to stop jquery function while navigating/reloading to other page?
I have used following jquery
functions to making Ajax
request periodically in rails
view 我已使用以下
jquery
函数在rails
视图中定期发出Ajax
请求
<script>
$(function() {
setInterval(function(){ $.get("/simulations/<%= @simulation.id %>"); }, 5000);
});
This functions works perfectly. 此功能运行完美。 But when I am navigating to other page by clicking back button it still running (confirmed by checking
chrome developer console
) 但是,当我通过单击“后退”按钮导航到其他页面时,该页面仍在运行(通过检查
chrome developer console
确认)
It stopped if i reload the page. 如果我重新加载页面,它停止了。 How to make it stop while navigating to other pages?
导航到其他页面时如何使其停止?
After suggestion 建议后
<script>
$(function() {
var interval = setInterval(function(){ $.get("/simulations/<%= @simulation.id %>"); }, 5000);
clearInterval(interval);
});
setInterval is a standard javascript function that returns a number representing an interval id that can be passed to clearInterval to be cancelled. setInterval是一个标准的javascript函数,它返回一个数字,该数字表示可以传递给clearInterval来取消的间隔ID。
<script>
$(function() {
var interval = setInterval(function(){ $.get("/simulations/<%= @simulation.id %>"); }, 5000);
// elsewhere ...
clearInterval(interval);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.