I am trying to understand this code:
function setIdle(cb, seconds) {
var timer;
var interval = seconds * 1000;
function refresh() {
clearInterval(timer);
timer = setTimeout(cb, interval);
};
$(document).on('keypress, click', refresh);
refresh();
}
setIdle(function() {
location.href = location.href;
}, 5);
setIdle
takes two arguments. Inside its function it has a function refresh
that clears a timer on a Timeout
function. Now every time when an event happens (click
, keypress
) refresh()
gets called.
and then finally this function gets called passing in another function and and int value (5
) which later will be the amount of seconds for the timer. In that other function which later is represented through cb
the page will be refreshed (location.href = location.href;
).
This causes an automaticpage refresh every 5 seconds.
So now I don't understand if I put an additional function:
setIdle(function() {
console.log('hi');
}, 1);
Why is the second function only called once and not every second like the other one?