前端倒计时js方法

用js 写的倒计时逻辑。

countDown(time) {
    let sTime = {
      min1: '',//十位分钟
      min2: '',//个位分钟
      second: '',//十位秒
      second2: '',//个位秒
    };
    var cut_time = time;
    let timer = setInterval(function() {
      // n < 10 ? '0' + n : '' + n
      sTime.min1 = Math.floor((cut_time / 60) / 10);
      sTime.min2 = Math.floor((cut_time / 60) % 10);
      sTime.second = parseInt(Math.floor(cut_time % 60) / 10); // 计算秒 十位
      sTime.second2 = Math.floor(cut_time % 60) % 10; // 计算秒 个位
      //修改UI值
      $('.min1').html(sTime.min1);
      $('.min2').html(sTime.min2);
      $('.sec1').html(sTime.second);
      $('.sec2').html(sTime.second2);
      if (cut_time < 0) {
        window.clearInterval(timer);
      }
      cut_time--;
    }, 1000);
  },


posted @ 2021-08-18 18:15  吃好每一口饭  阅读(298)  评论(0)    收藏  举报