微信小程序秒杀倒计时无限循环

本文由清尘发表于2020-06-21 11:41最后修改于2020-06-22属于移动前端分类
...
<view class="timebox">距结束
  <text class="spli"></text>
  <text class="time_text">{{count_down_hh}}</text>
  <text class="spli">:</text>
  <text class="time_text">{{count_down_mm}}</text>
  <text class="spli">:</text>
  <text class="time_text">{{count_down_ss}}</text>
</view>

...
Page({
  data:{
    count_down_hh: '',
    count_down_mm: '',
    count_down_ss: ''
  },
  countDown: function () {
    var time_start = new Date().getTime(),
        end = new Date();
    end.setHours(23),
        end.setMinutes(59),
        end.setSeconds(59);
    var time_end = end.getTime();
    var time_distance = time_end - time_start;
    var int_day = Math.floor(time_distance / 86400000);
    time_distance -= int_day * 86400000;
    var int_hour = Math.floor(time_distance / 3600000);
    time_distance -= int_hour * 3600000;
    var int_minute = Math.floor(time_distance / 60000);
    time_distance -= int_minute * 60000;
    var int_second = Math.floor(time_distance / 1000);
    if (int_day < 10) {
        int_day = "0" + int_day;
    }
    if (int_hour < 10) {
        int_hour = "0" + int_hour;
    }
    if (int_minute < 10) {
        int_minute = "0" + int_minute;
    }
    if (int_second < 10) {
        int_second = "0" + int_second;
    }

    this.setData({
        count_down_hh: int_hour,
        count_down_mm: int_minute,
        count_down_ss: int_second
    })

    setTimeout(() => {
        this.countDown();
    }, 1000);

},

  ...
})