最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Keep timer (setInterval) running while reloading page - Stack Overflow

programmeradmin2浏览0评论

Once I load a webpage, I insert some Javscript via the console. I was wondering if it's possible for me to, using either Javascript or jQuery, reload the page (not from cache) while keeping a setInterval that I have running. I'm familiar with location.reload(), but that terminates it.

Once I load a webpage, I insert some Javscript via the console. I was wondering if it's possible for me to, using either Javascript or jQuery, reload the page (not from cache) while keeping a setInterval that I have running. I'm familiar with location.reload(), but that terminates it.

Share Improve this question edited Apr 13, 2014 at 0:01 user2864740 62k15 gold badges157 silver badges227 bronze badges asked Apr 12, 2014 at 22:47 Ryan LuciaRyan Lucia 1021 silver badge10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

When you reload a page, the entire page context including all running JS is pletely destroyed. You cannot keep a setInterval() running while its host page is reloaded.

You can create a signal for the new page to start the interval going again itself using a cookie, query parameter or local storage value (query parameter is probably the most appropriate). If you go this way, then you need to code the page to look for a specific query parameter and if it finds it, then the page should start the designated setInterval() itself. You can even pass some data in the query parameter (such as how much more time until the next interval should fire, etc...).

Another option is to not actually reload the page, but instead to refresh the content manually by getting new content via an ajax call and then inserting it into the current page. This allowed the current page context and running interval timers to continue running.

Not possible unless you fetch the page using Ajax request, then replace the body while the setInterval Is working

发布评论

评论列表(0)

  1. 暂无评论