這篇文章主要為大家詳細介紹了微信小程序動態(tài)顯示項目倒計時,格式如4天7小時58分鐘39秒,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序動態(tài)顯示項目倒計時的具體代碼,供大家參考,具體內(nèi)容如下
1、一般我們說的顯示秒殺都是指的單條數(shù)據(jù),循環(huán)我沒做。
效果:
2、wxml代碼:
<p> <block wx:if="{{total_micro_second<=0}}">剩余時間:已經(jīng)截止</block> <block wx:if="{{clock!='已經(jīng)截止'}}">剩余時間:{{clock}}</block> </p>
登錄后復制
3、.js文件代碼:
function countdown(that) { var EndTime = that.data.end_time || []; var NowTime = new Date().getTime(); var total_micro_second = EndTime - NowTime || []; console.log('剩余時間:' + total_micro_second); // 渲染倒計時時鐘 that.setData({ clock: dateformat(total_micro_second) }); if (total_micro_second <= 0) { that.setData({ clock: "已經(jīng)截止" }); //return; } setTimeout(function () { total_micro_second -= 1000; countdown(that); } , 1000) } // 時間格式化輸出,如11:03 25:19 每1s都會調(diào)用一次 function dateformat(micro_second) { // 總秒數(shù) var second = Math.floor(micro_second / 1000); // 天數(shù) var day = Math.floor(second/3600/24); // 小時 var hr = Math.floor(second/3600%24); // 分鐘 var min = Math.floor(second/60%60); // 秒 var sec = Math.floor(second%60); return day + "天" + hr + "小時" + min + "分鐘" + sec+"秒"; } Page({ /** * 頁面的初始數(shù)據(jù) */ data: { id:'', result:[], end_time:'', clock:'' },/** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad: function (options) { var that = this; wx.request({ url: 'https://m.******.com/index.php/Home/Xiaoxxf/activity_detail?a_id='+options.id,//不含富文本html data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT header: { 'Content-Type': 'application/json' }, success: function (res) { that.setData({ common: res.data, //一維數(shù)組,全部數(shù)據(jù) end_time: res.data.end_time //項目截止時間,時間戳,單位毫秒 }) console.log(that.data.common); console.log('結(jié)束時間:' + that.data.end_time); }, fail: function (res) { }, complete: function (res) { }, }), //調(diào)用上面定義的遞歸函數(shù),一秒一刷新時間 countdown(that); },
登錄后復制
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,更多相關內(nèi)容請關注PHP中文網(wǎng)!
相關推薦:
以上就是微信小程序動態(tài)顯示項目倒計時的效果的詳細內(nèi)容,更多請關注有卡有網(wǎng)。