Pause setTimeout when leaving the window
Posted: 21 Nov 2020, 10:51
The code below sends the 30s event do Google Analytics after 30 seconds a user enters a page.
But when the user minimizes the window, the event still fires.
What I want is a way to "pause" the setTimeout when the user minimizes the page and, when he maximizes the page, the setTimeout continues counting from the moment it has stopped.
Any way to do that?
Code: Select all
setTimeout(function(){
gtag('event', '30s');
}, 30000);
What I want is a way to "pause" the setTimeout when the user minimizes the page and, when he maximizes the page, the setTimeout continues counting from the moment it has stopped.
Any way to do that?