$(window).blur event affecting Iframe

前端 未结 3 462
一整个雨季
一整个雨季 2020-12-16 04:17

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
}         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 05:09

    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();     
      }
      
    }

提交回复
热议问题