Reputation: 45
I need to show popup if tab with this script was in background one minute(delay). This code works but there is a problem. If i open page in background tab, this doesnt work. I see in console "1" written two times and "2" written 1 time after I went first time on background tab. I think I need some check with function document.hasFocus() so that my idea would work good. How to do it?
var showPopupTimeout;
$(window).blur(function () {
if (!checkCompleted) {
console.log("2");
showPopupTimeout = setTimeout(checkClientCalled, delay);
}
});
$(window).focus(function () {
if (!checkCompleted) {
console.log("1");
clearTimeout(showPopupTimeout);
}
});
Upvotes: 2
Views: 334
Reputation: 55729
Use the page visibility API.
Listen to "onvisibilitychange". If the page is "hidden" then record the time.
If the page is "visible" then check the time the page was hidden. Subtract it from now and if the difference is more than 60000ms then show the dialog.
Upvotes: 1