1
function process(){
  window.location.reload(false);
}

function looping(){
  setTimeout(process(), 10000);
}

var looper = setInterval(looping(), 10000);

I'm trying to reload a page every 10 seconds, but the above code will too frequently reload the page. Why?

luoyianwu
  • 13
  • 1
  • 4
  • 3
    because you're invoking `looping()` immediately ... remove the () from looping() in setInterval ... there's similar issues with the way you're calling process() ... and, well, using setTimeout and setInterval like that seems redundant – Jaromanda X Jan 25 '16 at 07:47
  • Another thing to keep in mind is that JavaScript will never be exact here. It has one event loop that it will execute from a queue, and it can't guarantee that things in it comes at specific times. It puts your callback in the loop and if there for example is heavy computing before in the loop you will have to wait. – William Jan 25 '16 at 07:55
  • Perhaps a better duplicate: http://stackoverflow.com/questions/11403357/why-do-i-have-to-omit-parentheses-when-passing-a-function-as-an-argument – Brock Adams Jan 25 '16 at 07:59

3 Answers3

4
function process(){
  window.location.reload(false);
}

function looping(){
  setTimeout(process, 10000);
}

var looper = setInterval(looping, 10000);

try above code.

in your example, instead of providing a call back function to the setTimeout and setInterval, you were, just calling the callback function. just provide the function name and its fixed

UPDATE: Functions are first class objects in JS. you can pass it to a function, return a function from another function etc... so those things are done, by just using the function name(just the function name, like any other variable name). invoking a function is done using the parenthesis, you were by mistake invoking the function, instead of passing the function to setTimeout

and you can get rid of the setTimeout completely and just do as shown below

function process(){
      window.location.reload(false);
}       

var looper = setInterval(process, 10000);  
Oxi
  • 2,918
  • 17
  • 28
1

The problem with your code is, you are invoking the function instead of passing it to be called after the time interval. If you need to pass the function, just pass the name of the function. Putting the parentheses is actually invoking the function which will happen right away the moment you set the setInterval

If you want to reload your page every 10 seconds, you can simplify it a bit. I don't see any reason why you should have setInterval in this case. I think this is sufficient and should work

// After 10s, run the function
setTimeout(function() { window.location.reload(false)},10000)

You might want the 10s counted after all the DOM is ready.

var fn = function() { 
  setTimeout(function() { window.location.reload(false)},10000);
}

// Note that, I pass the function name instead of invoking it with fn()
document.addEventListener('DOMContentLoaded', fn)

The reason why I am using setTimeout instead of setInterval is, it will only run once per session and will be refreshed. I think it should be sufficient.

geckob
  • 7,680
  • 5
  • 30
  • 39
0

process() this will call function and process will return function. Hence your function is called immediately.

try

setTimeout(process,10000)

Rajesh
  • 24,354
  • 5
  • 48
  • 79