怎么在微信小程序中實(shí)現(xiàn)秒殺批量倒計(jì)時(shí)功能

這篇文章將為大家詳細(xì)講解有關(guān)怎么在微信小程序中實(shí)現(xiàn)秒殺批量倒計(jì)時(shí)功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)建站是一家專注于成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),達(dá)拉特網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:達(dá)拉特等地區(qū)。達(dá)拉特做網(wǎng)站價(jià)格咨詢:18980820575

微信小程序?qū)崿F(xiàn)倒計(jì)時(shí),可以將倒計(jì)時(shí)的時(shí)間進(jìn)行每一秒的計(jì)算和渲染!

JS

  1. 模擬商品列表數(shù)據(jù) goodsList;

  2. 在 onLoad 周期函數(shù)中對(duì)活動(dòng)結(jié)束時(shí)間進(jìn)行提??;

  3. 建立時(shí)間格式化函數(shù) timeFormat;

  4. 建立倒計(jì)時(shí)函數(shù) countDown;

  5. 在 onLoad 周期函數(shù)的提取結(jié)尾執(zhí)行倒計(jì)時(shí)函數(shù) countDown。

倒計(jì)時(shí)函數(shù)詳解

  1. 獲取當(dāng)前時(shí)間,同時(shí)得到活動(dòng)結(jié)束時(shí)間數(shù)組;

  2. 循環(huán)活動(dòng)結(jié)束時(shí)間數(shù)組,計(jì)算每個(gè)商品活動(dòng)結(jié)束時(shí)間的倒計(jì)時(shí)天、時(shí)、分、秒;

  3. 用 setData 方法刷新數(shù)據(jù);

  4. 每個(gè)一秒執(zhí)行一次倒計(jì)時(shí)函數(shù) setTimeout(this.countDown,1000);

let goodsList = [
 {actEndTime: '2018-05-01 10:00:43'},
 {actEndTime: '2018-04-01 11:00:00'},
 {actEndTime: '2018-06-01 12:45:56'},
 {actEndTime: '2018-07-01 15:00:23'},
 {actEndTime: '2018-05-23 17:00:22'},
 {actEndTime: '2018-05-14 19:00:44'},
 {actEndTime: '2018-05-21 21:00:34'},
 {actEndTime: '2018-06-17 09:00:37'},
 {actEndTime: '2018-03-21 05:00:59'},
 {actEndTime: '2018-04-19 07:00:48'},
 {actEndTime: '2018-04-28 03:00:11'}
]
Page({
 data: {
 countDownList: [],
 actEndTimeList: []
 },
 onLoad(){
 let endTimeList = [];
 // 將活動(dòng)的結(jié)束時(shí)間參數(shù)提成一個(gè)單獨(dú)的數(shù)組,方便操作
 goodsList.forEach(o => {endTimeList.push(o.actEndTime)})
 this.setData({ actEndTimeList: endTimeList});
 // 執(zhí)行倒計(jì)時(shí)函數(shù)
 this.countDown();
 },
 timeFormat(param){//小于10的格式化函數(shù)
 return param < 10 ? '0' + param : param; 
 },
 countDown(){//倒計(jì)時(shí)函數(shù)
 // 獲取當(dāng)前時(shí)間,同時(shí)得到活動(dòng)結(jié)束時(shí)間數(shù)組
 let newTime = new Date().getTime();
 let endTimeList = this.data.actEndTimeList;
 let countDownArr = [];

 // 對(duì)結(jié)束時(shí)間進(jìn)行處理渲染到頁(yè)面
 endTimeList.forEach(o => {
  let endTime = new Date(o).getTime();
  let obj = null;
  // 如果活動(dòng)未結(jié)束,對(duì)時(shí)間進(jìn)行處理
  if (endTime - newTime > 0){
  let time = (endTime - newTime) / 1000;
  // 獲取天、時(shí)、分、秒
  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{//活動(dòng)已結(jié)束,全部設(shè)置為'00'
  obj = {
   day: '00',
   hou: '00',
   min: '00',
   sec: '00'
  }
  }
  countDownArr.push(obj);
 })
 // 渲染,然后每隔一秒執(zhí)行一次倒計(jì)時(shí)函數(shù)
 this.setData({ countDownList: countDownArr})
 setTimeout(this.countDown,1000);
 }
})

WXML

簡(jiǎn)單的布局和居中顯示。

<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>時(shí)
 <text class='tui-conutdown-box'>{{item.min}}</text>分
 <text class='tui-conutdown-box tui-countdown-bg'>{{item.sec}}</text>秒
</view>

WXSS

page{background-color: #eee;}
.tui-countdown-content{
 height: 50px;
 line-height: 50px;
 text-align: center;
 background-color: #fff;
 margin-top: 15px;
 padding: 0 15px;
 font-size: 18px;
}
.tui-conutdown-box{
 display: inline-block;
 height: 26px;
 width: 26px;
 line-height: 26px;
 text-align: center;
 background-color: #000;
 color: #fff;
 margin: 0 5px;
}
.tui-countdown-bg{
 background-color: #DF0101;
}

關(guān)于怎么在微信小程序中實(shí)現(xiàn)秒殺批量倒計(jì)時(shí)功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前題目:怎么在微信小程序中實(shí)現(xiàn)秒殺批量倒計(jì)時(shí)功能
標(biāo)題來(lái)源:http://muchs.cn/article48/iepgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、微信小程序網(wǎng)站導(dǎo)航、Google品牌網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)