小程序頁面搶購時間倒計時


注意:

  •   設置一個時間段,幾點到幾點倒計時
  •   比如第一次進入頁面是10秒,你出頁面后3秒,第二次進入頁面也在繼續倒計時,這時候的時間是7秒,繼續倒計時
      <view class='tui-countdown-content' wx:for="{{countDownList}}" wx:key="countDownList">
          <!-- <text class='tui-conutdown-box'>{{item.day}}</text>天 -->
          <text class='tui-conutdown-box'>{{item.hou}}</text>:
          <text class='tui-conutdown-box'>{{item.min}}</text>:
          <text class='tui-conutdown-box tui-countdown-bg'>{{item.sec}}</text>
        </view>

let goodsList = [
  { actEndTime: '2019/03/26 20:00:00' }
]
Page({

  /**
   * 頁面的初始數據
   */
  data: {
    countDownList: [],
    actEndTimeList: []
  },

  /**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
    let endTimeList = [];
    // 將活動的結束時間參數提成一個單獨的數組,方便操作
    goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
    this.setData({ actEndTimeList: endTimeList });
    // 執行倒計時函數
    this.countDown();
  },
  timeFormat(param) {//小於10的格式化函數
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒計時函數
    // 獲取當前時間,同時得到活動結束時間數組
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 對結束時間進行處理渲染到頁面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime();
      let obj = null;
      // 如果活動未結束,對時間進行處理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 獲取天、時、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活動已結束,全部設置為'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj);
    })
    // 渲染,然后每隔一秒執行一次倒計時函數
    this.setData({ countDownList: countDownArr })
    setTimeout(this.countDown, 1000);
  },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM