微信小程序中顯示倒計時代碼實例
更新時間:2019年05月09日 16:04:29 作者:MacleChen
這篇文章主要介紹了微信小程序中顯示倒計時,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
wxml文件中:
<!--倒計時 -->
<view class="countDownTimeView countDownAllView" >
<view class="voteText countDownTimeText">{{countDownDay}}天</view>
<view class="voteText countDownTimeText">{{countDownHour}}時</view>
<view class="voteText countDownTimeText">{{countDownMinute}}分</view>
<view class="voteText countDownTimeText">{{countDownSecond}}秒</view>
</view>
js文件中:
Page( {
data: {
windowHeight: 654,
maxtime: "",
isHiddenLoading: true,
isHiddenToast: true,
dataList: {},
countDownDay: 0,
countDownHour: 0,
countDownMinute: 0,
countDownSecond: 0,
},
//事件處理函數(shù)
bindViewTap: function() {
wx.navigateTo( {
url: '../logs/logs'
})
},
onLoad: function() {
this.setData( {
windowHeight: wx.getStorageSync( 'windowHeight' )
});
},
// 頁面渲染完成后 調(diào)用
onReady: function () {
var totalSecond = 1505540080 - Date.parse(new Date())/1000;
var interval = setInterval(function () {
// 秒數(shù)
var second = totalSecond;
// 天數(shù)位
var day = Math.floor(second / 3600 / 24);
var dayStr = day.toString();
if (dayStr.length == 1) dayStr = '0' + dayStr;
// 小時位
var hr = Math.floor((second - day * 3600 * 24) / 3600);
var hrStr = hr.toString();
if (hrStr.length == 1) hrStr = '0' + hrStr;
// 分鐘位
var min = Math.floor((second - day * 3600 *24 - hr * 3600) / 60);
var minStr = min.toString();
if (minStr.length == 1) minStr = '0' + minStr;
// 秒位
var sec = second - day * 3600 * 24 - hr * 3600 - min*60;
var secStr = sec.toString();
if (secStr.length == 1) secStr = '0' + secStr;
this.setData({
countDownDay: dayStr,
countDownHour: hrStr,
countDownMinute: minStr,
countDownSecond: secStr,
});
totalSecond--;
if (totalSecond < 0) {
clearInterval(interval);
wx.showToast({
title: '活動已結(jié)束',
});
this.setData({
countDownDay: '00',
countDownHour: '00',
countDownMinute: '00',
countDownSecond: '00',
});
}
}.bind(this), 1000);
},
//cell事件處理函數(shù)
bindCellViewTap: function (e) {
var id = e.currentTarget.dataset.id;
wx.navigateTo({
url: '../babyDetail/babyDetail?id=' + id
});
}
})
效果圖:

以上所述是小編給大家介紹的微信小程序中顯示倒計時詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript字符串_動力節(jié)點Java學院整理
JavaScript中的字符串就是用''或""括起來的字符表示。下面通過本文給大家介紹JavaScript字符串的相關(guān)知識,感興趣的朋友一起看看吧2017-06-06
詳細談?wù)凧avaScript中循環(huán)之間的差異
JS循環(huán)語句也叫迭代語句,是一種特殊的語句,主要用于需要多次執(zhí)行的代碼塊,下面這篇文章主要給大家介紹了關(guān)于JavaScript中循環(huán)之間的差異的相關(guān)資料,需要的朋友可以參考下2021-08-08

