JS 倒计时

 

记录一下,直接贴方法了:

  changeTime(ms) {
      if (!ms) return;
      var time = new Date(ms);
      function c(a, b) {
        var x = Math.floor(ms / a % b);
        if (x < 10) {
          return '0' + x;
        }
        return '' + x;
      }
      var a = (c(3600000, 24) + c(60000, 60) + c(1000, 60)).split("");
      var day = "";
      var day2 = "";
      if (ms > 86400000 && ms < 360000000) {
        day = '' + Math.floor(ms / 86400000) * 24;
        day2 = day.split('');
        var a0 = (+a[0]) + (+day2[0]),
          a1 = (+a[1]) + (+day2[1]);
        if (a1 >= 10) {
          a0 = a0 + Math.floor(a1 / 10);
          a1 = Math.floor(a1 % 10);
        }
        return a0 + "" + a1 + "时" + a[2] + "" + a[3] + "分" + a[4] + "" + a[5] + '秒'
      } else if (ms >= 360000000) {
        day = Math.floor(ms / 86400000) + "天";
        return day + "" + a[0] + "" + a[1] + "时" + a[2] + "" + a[3] + "分" + a[4] + "" + a[5] + '秒'
      } else {
        return day + "" + a[0] + "" + a[1] + "时" + a[2] + "" + a[3] + "分" + a[4] + "" + a[5] + '秒'
      }
    }

 

changeTime 传的参数是结束时间和当前时间的毫秒差,完事再写个定时器就行了。

 

posted @ 2019-08-20 19:17  _whys  阅读(254)  评论(0编辑  收藏  举报