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

Javascript location.reload() in setInterval function is not working as expected - Stack Overflow

programmeradmin1浏览0评论

I'm making a chrome extension whose sole purpose is to prevent session timeout.For that I'm using this mand:

setInterval(function(){ location.reload(); }, 10000);

What I'm expecting is a page refresh for every 10 seconds which is not happening. But when I write this :

setInterval(function(){ alert("Hello"); }, 3000);

It is showing hello for every 3 seconds where as setInterval(function(){ location.reload(); }, 10000); is refreshing page just for one time after ten minutes. What might be the error in this ?

I'm making a chrome extension whose sole purpose is to prevent session timeout.For that I'm using this mand:

setInterval(function(){ location.reload(); }, 10000);

What I'm expecting is a page refresh for every 10 seconds which is not happening. But when I write this :

setInterval(function(){ alert("Hello"); }, 3000);

It is showing hello for every 3 seconds where as setInterval(function(){ location.reload(); }, 10000); is refreshing page just for one time after ten minutes. What might be the error in this ?

Share Improve this question asked May 6, 2017 at 6:57 BharatBharat 1,0822 gold badges16 silver badges34 bronze badges 2
  • 2 location.reload() is a browser refresh; the old page is discarded, along with its running JavaScript. – Ry- Commented May 6, 2017 at 6:59
  • 1 But how could I acheive the above task then? – Bharat Commented May 6, 2017 at 7:01
Add a ment  | 

1 Answer 1

Reset to default 7

I don't remend you to use this kind of code because every client try to load all data every 3 seconds and it puts extra pressure to the server. you can make a real-time bidirectional munication. for example socket.io can help you to make it easily. But if it isn't possible for you try this code:

    setTimeout(function() {
       window.location.href = window.location;
    }, 3000);
发布评论

评论列表(0)

  1. 暂无评论