I want to detect when the user leaves my page( e.g open a new tab) so I can stop a countdown. I did it using:
$(window).blur(function() {
//stop countdown
}
I have the same problem. in my case, I haven't access to iframe page and its load by CMS and I can't change all of the iframes. my timer counting with setInterval() and inside the interval, I check the Iframe.
const focus = function() {
// timer start
isBlured = 0;
};
const blur = function() {
// timer stop
isBlured = 1;
}
window.addEventListener('focus', focus);
window.addEventListener('blur', blur);
function intervalFunction() {
var activeELM = document.activeElement.tagName;
if (isBlured == 0 || activeELM == "IFRAME"){
// dont stop countdown
var stopIframe = $('iframe').blur();
}
}