duande8497 2016-02-19 05:46
浏览 262

在浏览器中的标签之间切换时停止计时器

Here is my script written
I want to stop timer when user switch to different tabs and resume back when visit the site. can anyone help me to solve this task.

$(document).ready(function() {   
      window.setInterval(function() { 
      var timeLeft    = $("#timeLeft").html();                                 
          if(eval(timeLeft) == 0){ 
                  window.location= ($("#url_online").html());                  
          }else{               
              $("#timeLeft").html(eval(timeLeft)- eval(1)); 
          } 
      }, 1000);  
      
  }); 
  
  var time=$("#times").html(); 
  var tt=time.split(":"); 

  var seconds =tt[0]*60+tt[1]*1; 
  function secondPassed() { 
      var minutes = Math.round((seconds - 30)/60); 
      var remainingSeconds = seconds % 60; 
      if (remainingSeconds < 10) { 
          remainingSeconds = "0" + remainingSeconds;   
      } 
      document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds; 
      if (seconds == 0) { 
          clearInterval(countdownTimer); 
          document.getElementById('countdown').innerHTML = "Buzz Buzz"; 
      } else { 
          seconds--; 
      } 
  } 
    
  var countdownTimer = setInterval('secondPassed()', 1000);
<p id="times"> 2:50</p>
Redirect You In  <span id="timeLeft">40</span> secs.. 

</div>
  • 写回答

2条回答 默认 最新

  • duangan9251 2016-02-19 06:11
    关注

    You can use $(window).focus() and $(window).blur() event.

    Example:

    https://jsfiddle.net/72cLu8c0/

    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺