2020年7月14日

无限循环倒计时

作者 rourou
  var timer; 
  var bTime = new Date("2020/07/15 00:00:00");
  var time_interval = 30;//间隔时间
  function timerBox(){
    var endTime=new Date(),
      nowTime = parseInt((endTime.getTime()-bTime.getTime())/1000),
      n=parseInt(nowTime/3600/24/time_interval),
      leftSecond=time_interval*24*3600-(nowTime-time_interval*n*24*3600),
      day=parseInt(leftSecond/3600/24),
      hour=parseInt((leftSecond/3600)%24),
      minite=parseInt((leftSecond/60)%60),
      second=parseInt(leftSecond%60);
      day = formatTime(day);
      hour = formatTime(hour);
      minite = formatTime(minite);
      second = formatTime(second);
      if(leftSecond>0){
          $("#dd").html(day);
          $("#hh").html(hour);
          $("#mm").html(minite);
          $("#ss").html(second);
          timer = setTimeout(timerBox,1000);
      }else{
        clearInterval(timer)
      };
    };

    function formatTime(date){
        if(date<10){
            date= "0" + date;
            return date;
        }else{
            return date;
        }
    }
    timerBox();