2020年8月24日

多个倒计时

作者 rourou
  function countDown(maxtime, fn) {
      var timer = setInterval(function () {
        var bTime = new Date().getTime();
        if (maxtime-bTime>0) {
          var leftSecond = parseInt(maxtime - bTime) / 1000,
            minite = parseInt((leftSecond / 60) % 60),
            second = parseInt(leftSecond % 60);
          minite = formatTime(minite);
          second = formatTime(second);
          msg = {
            minite: minite,
            second: second
          };
          fn(msg);
          --maxtime;
        } else {
          clearInterval(timer);//订单结束
        }
      }, 1000);
    }

    var aTime = [new Date('2020/08/24 14:55').getTime(), new Date('2020/08/24 15:10').getTime()];//结束时间数组时间挫
    var countdown = $('.order_course_prc');
    for (var i = 0; i < countdown.length; i++) {
      (function (i) {
        countDown(aTime[i], function (msg) {
          countdown.eq(i).find(".mm").html(msg.minite);
          countdown.eq(i).find(".ss").html(msg.second);
        })
      })(i)
    }

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