简体   繁体   English

setinterval在ajax加载后保持运行

[英]setinterval keeps running after ajax load

I load content in to a DIV (#mydiv) from a page (mypage.php) . 我从页面(mypage.php)将内容加载到DIV(#mydiv)中。 The page that i get content from has javascript inside. 我从中获取内容的页面具有javascript内部。

In THE mypage.php 在mypage.php中

 function refresh_feeds() {
//bla bla
}

$(function(){

    feed_timer = setInterval(refresh_feeds, 50000);

});

When i clean the content of #mydiv and load content from another page (mypage2.php), setInterval that started in mypage.php keeps running. 当我清理#mydiv的内容并从另一个页面(mypage2.php)加载内容时,从mypage.php开始的setInterval继续运行。

my question is how can i stop it when the content from mypage.php is unloaded 我的问题是,当mypage.php中的内容卸载后,如何停止它

or 要么

how can i handle unload event of mypage.php ? 我该如何处理mypage.php的卸载事件?

var feed_timer;
function refresh_feeds() {
    //bla bla
}

$(function(){
    clearInterval(feed_timer);
    feed_timer = setInterval(refresh_feeds, 50000);
});

you have to clear your Interval 您必须清除间隔

clearInterval(feed_timer);

be sure to save the feed_timer in a global scope 确保将feed_timer保存在全局范围内

function refresh_feeds() {
  console.log(1);
}

$(function() {
  window.feed_timer = window.setInterval(refresh_feeds, 100);
});

$(window).bind("beforeunload",function() {
  window.clearInterval(window.feed_timer);
  window.feed_timer = null;
  return 1;
});

Please check in console for this 请在控制台中检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM