The problem is with the behaviour of the event \"visibilitychange\".
It\'s triggered: - When I switch to a different tab inside the browser window.
There's a very simple solution to this I have come across.
You just need to pass false to the useCapture while attaching an event listener to the document. Works like a charm!
document.addEventListener('visibilitychange', function () { // code goes here }, false)