我需要一个同时工作的倒数计时器

时间:2018-09-06 22:51:04

标签: javascript countdown

  1. 嗨,大家好?请我需要帮助,我想要一种情况 在特定时间,第一个计时器应停止计数,而第二个计时器 计时器执行并更新“ BREAK TIME”,其方式是 刷新,直到第二个计时器完成执行,第一个计时器才执行,反之亦然。

        

    <script>
    
    
     let element = document.querySelector('#countdown');
        let breakTime = document.querySelector("#countdownbreak"),myD=true;
        function TimeFii(){
          time_difference = (60*1) -(((Date.now()-localStorage.getItem('realTime'))/1000) | 0); //60 if duration = 60*1
          hours = (time_difference/3600) | 0;
          minutes = ((time_difference % 36000) / 60)  | 0;
          seconds = (time_difference %60)|0;
          hours = hours<10 ? "0"+hours:hours
          minutes =  minutes < 10 ? "0"+minutes:minutes//00
          seconds = seconds <10 ?"0"+seconds:seconds
            uniseconds = seconds;
            element.textContent = minutes+":"+seconds;
            if(time_difference<=0){
              var start = Date.now()+100;`enter code here`
              localStorage.setItem('realTime',start);
            }
            if(seconds==39){
              stop();
              resetFii();
              element.innerHTML =""
              localStorage.setItem('noStart',myD)
              startFiiTimer(TimeFiiBreak);
            }
        }
    
        function TimeFiiBreak(){
          time_difference = (60*3) -(((Date.now()-localStorage.getItem('realBTime'))/1000) | 0); //60 if duration = 60*1
          hours = (time_difference/3600) | 0;
          minutes = ((time_difference % 36000) / 60)  | 0;
          seconds = (time_difference %60)|0;
          hours = hours<10 ? "0"+hours:hours
          minutes =  minutes < 10 ? "0"+minutes:minutes//00
          seconds = seconds <10 ?"0"+seconds:seconds
            uniseconds = seconds;
            breakTime.textContent = "BREAK TIME:"+minutes+":"+seconds;
            if(time_difference<=0){
              var start = Date.now()+100;
              localStorage.setItem('realBTime',start);
            }
            if(seconds==40){
              stopB();
              resetBFii();
              startFiiTimer(TimeFii);
    
            }
        }
    
        function startFiiTimer(fun){
          return setInterval(fun,100);
        }
    
    
        function resetFii(){
            return localStorage.removeItem('realTime');
        }
        function stop(){
          return clearInterval(TimeFii)
        }
    
        function resetBFii(){
            return localStorage.removeItem('realBTime');
        }
        function stopB(){
          return clearInterval(TimeFii)
        }
        window.onload = function (){
          if(myD !=false){
            startFiiTimer(TimeFii);
          }
        }
    
    </script>
    

    请运行上面的代码段,但不完全如我所愿,您可以访问fastplay24.com来查看计时器示例。谢谢

1 个答案:

答案 0 :(得分:0)

setInterval()返回一个计时器ID,在调用clearInterval()时需要使用该ID,而不是函数本身。像这样:

idtimerfii = setInterval(fun,100);

clearInterval(idtimerfii)