微信小程序?qū)崿F(xiàn)商城倒計(jì)時(shí)
本文實(shí)例為大家分享了微信小程序?qū)崿F(xiàn)商城倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
index.html:
<view class="countDownTimeView pull-left countDownAllView text-left"> <text>倒計(jì)時(shí):</text> <text class="voteText countDownTimeText">{{countDownHour}}</text> : <text class="voteText countDownTimeText">{{countDownMinute}}</text> : <text class="voteText countDownTimeText">{{countDownSecond}}</text> </view>
util.js :
const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } module.exports = { formatTime: formatTime }
index.js:
var util = require('../../utils/util.js'); //調(diào)用微信小程序中時(shí)間格式化的js Page: ({ data: { countDownHour: 0, //倒計(jì)時(shí) -時(shí) countDownMinute: 0, //倒計(jì)時(shí) -分 countDownSecond: 0, //倒計(jì)時(shí)-秒 }, // 頁(yè)面渲染后 執(zhí)行 onLoad: function () { //設(shè)置倒計(jì)時(shí)時(shí)間,1s變換一次 var interval = setInterval(function () { var d = new Date(); //獲取系統(tǒng)日期和時(shí)間 var nowHour = d.getHours(); //小時(shí) var nowMinutes = d.getMinutes(); //分 var nowSeconds = d.getSeconds(); //秒 // 顯示在倒計(jì)時(shí)中的小時(shí)位 var hour = 24 - nowHour; // 顯示在倒計(jì)時(shí)中的分鐘位 var minutes = 60 - nowMinutes; // 顯示在倒計(jì)時(shí)中的秒數(shù) var seconds = 60 - nowSeconds; //當(dāng)小時(shí)、分鐘、秒都為0時(shí),活動(dòng)結(jié)束,倒計(jì)時(shí)顯示為00:00:00 if (hour == 0 && minutes == 0 && seconds == 0) { clearInterval(interval); wx.showToast({ title: '活動(dòng)已結(jié)束', }); console.log(totalSecond); this.setData({ countDownHour: '00', countDownMinute: '00', countDownSecond: '00', }); } //當(dāng)小時(shí)位、分鐘位、秒位小于10時(shí),用字符串拼接的方式顯示,例如:06:08:02 if (hour < 10) { hour = "0" + hour; } if (minutes < 10) { minutes = "0" + minutes; } if (seconds < 10) { seconds = "0" + seconds; } this.setData({ countDownHour: hour, countDownMinute: minutes, countDownSecond: seconds, }); }.bind(this), 1000); }, })
最終實(shí)現(xiàn)效果圖如下:
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序?qū)崿F(xiàn)數(shù)字滾動(dòng)動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)數(shù)字滾動(dòng)動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

js純數(shù)字逐一停止顯示效果的實(shí)現(xiàn)代碼

JavaScript中內(nèi)置函數(shù)Map()的使用

jstree中的checkbox默認(rèn)選中和隱藏示例代碼

js將鍵值對(duì)字符串轉(zhuǎn)為json字符串的方法

JS實(shí)現(xiàn)中英文混合文字溢出友好截取功能

javascript刪除數(shù)組重復(fù)元素的方法匯總