Attempting to implement a looping method on a webpage (such as Facebook) like this:
function loopRefresh () {
console.log("loop refresh method call");
if (shallRefresh){
setTimeout(function(){
loopRefresh()
//Do something here
}, 5000);
} else {
setTimeout(function(){
loopRefresh()
//Do something different here
}, 5000);
}
}
Initially, everything seems to be functioning properly as the method is called every 5 seconds. However, an issue arises when the user clicks on the home button: https://i.sstatic.net/L7Bl8.png
This results in the page being reloaded due to the anchor tag and href, even though it doesn't lead to a new page, consequently interrupting the loop.
An onclick event has already been added to the Home Button calling this function:
function homeRefresh() {
shallRefresh = true;
//setTimeout(function(){
// Do the same thing here as in the true case in the loopRefresh method
// }, 2000);
}
The initial idea was to include the setTimeout call within this function so that the callback function would execute after the user clicked the button without involving the loopRefresh method. Unfortunately, attempting to pass the variable proved ineffective in resolving the issue.