I do not need help with a soloution here
My code works but I am trying to understand the difference between two soloutions that give diffirent results even though one would think they would give the same result.
I have a page that I update using setinterval.
This page gets updated every 60 seconds(code will folow below). I had a problem where when I changed tab and came back the page composated the missing time by updating the page multiple times.
I googled and found that detecting the focus and blur event on the window would solve my problem. This in fact did solve the problem but also created a new one.
On focus I created a new Interval:
window.addEventListener('focus', function () {
$.OverWatch.worker.setUpdateInterval()
}, false);
And on blur I cleared all intervals on the page:
window.addEventListener('blur', function () {
$.OverWatch.worker.stopUpdateInterval()
}, false);
This created a new issue. Once I changed the tab and came back to the page I had to wait another 60 seconds till the update took effect.
So i thought I could implement logic that updates the page on focus and creates a new interval:
Attempt 1
window.addEventListener('focus', function () {
$.OverWatch.worker.getView("/OverWatch/UpdateWatch", function () { // ajax updates page
// inside of success function
$.OverWatch.init();
$.OverWatch.worker.setUpdateInterval()
});
}, false);
This worked but It took me back to my original problem where the browser ran the code multiple times.
After searching the web I found that i could set a function to the window.Onfocus event:
Attempt 2
window.onfocus = function () {
$.OverWatch.worker.getView("/OverWatch/UpdateWatch", function () {
// inside of success function
$.OverWatch.init();
$.OverWatch.worker.setUpdateInterval()
});
}
This works perfectly but I don't understand why does this works without multiple page updates and not the code in attempt 1? What is the difference between these two?