Javascript location.reload() in setInterval function is not working as expected
Asked Answered
G

1

6

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

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 ?

Graehme answered 6/5, 2017 at 6:57 Comment(2)
location.reload() is a browser refresh; the old page is discarded, along with its running JavaScript.Ultimogeniture
But how could I acheive the above task then?Graehme
M
7

I don't recommend 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 communication. 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);
Mammalogy answered 6/5, 2017 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.