The following script is supposed to do the following:
At page load, it will set a timeout of 20 seconds, then the desired content will be updated in an ajax call automatically.
If the user clicks a button that says RELOAD, it will call to function reloadContent()
and thus clearing the timeout and setting one again.
So basically, it will should restart the timeout if the user manually reloaded, to avoid too close calls.
It works half, because the timer seems to be reset, but the reloadContentTimeout()
will execute after 10 seconds of the manually loaded action, which is half the timeout time.
Am I doing something wrong?
Thanks!
/**
* Reload content
*/
function reloadContent(elementId) {
$(elementId).load(window.location.href+' '+elementId+' > *');
clearTimeout(timeoutId);
reloadContentTimeout(false);
}
/**
* Reload content every X seconds (20)
*/
var timeoutId;
function reloadContentTimeout(now)
{
now = (typeof now === 'undefined' ? true : now);
if( now ) {
reloadContent('#table-content');
}
timeoutId = setTimeout(reloadContentTimeout, 1000 * 20);
}
reloadContentTimeout(false);