微信小程序電商常用倒計(jì)時(shí)實(shí)現(xiàn)實(shí)例
微信小程序電商常用倒計(jì)時(shí)實(shí)現(xiàn)實(shí)例
wxml文件放個(gè)text
<text>second: {{second}} micro second:{{micro_second}}</text>
在js文件中調(diào)用
function countdown(that) {
var second = that.data.second
if (second == 0) {
// console.log("Time Out...");
that.setData({
second: "Time Out..."
});
return ;
}
var time = setTimeout(function(){
that.setData({
second: second - 1
});
countdown(that);
}
,1000)
}
Page({
data: {
second: 3
},
onLoad: function() {
countdown(this);
}
});
運(yùn)行驗(yàn)證下,從10走到1s,然后顯示時(shí)間到。
于是繼續(xù)將毫秒完善,注意毫秒的步長(zhǎng)受限于系統(tǒng)的時(shí)間頻率,于是我們精確到0.01s即10ms
js
/* 秒級(jí)倒計(jì)時(shí) */
function countdown(that) {
var second = that.data.second
if (second == 0) {
that.setData({
second: "Time out!",
micro_second: "micro_second too."
});
clearTimeout(micro_timer);
return ;
}
var timer = setTimeout(function(){
that.setData({
second: second - 1
});
countdown(that);
}
,1000)
}
/* 毫秒級(jí)倒計(jì)時(shí) */
// 初始毫秒數(shù),同時(shí)用作歸零
var micro_second_init = 100;
// 當(dāng)前毫秒數(shù)
var micro_second_current = micro_second_init;
// 毫秒計(jì)時(shí)器
var micro_timer;
function countdown4micro(that) {
if (micro_second_current <= 0) {
micro_second_current = micro_second_init;
}
micro_timer = setTimeout(function(){
that.setData({
micro_second: micro_second_current - 1
});
micro_second_current--;
countdown4micro(that);
}
,10)
}
Page({
data: {
second: 2,
micro_second: micro_second_init
},
onLoad: function() {
countdown(this);
countdown4micro(this);
}
});
wxml文件
<text style="display: block;">second: {{second}}s</text>
<text>{{micro_second}}</text>
如此,當(dāng)秒級(jí)運(yùn)行完畢時(shí),毫秒級(jí)timer即clearTimeout,并將字本顯示為'micro_second too'
再添加一個(gè)countdown4micro方法,使得顯示剩余 0:3:19 89這樣形式的倒數(shù)
function dateformat(second) {
var dateStr = "";
var hr = Math.floor(second / 3600);
var min = Math.floor((second - hr * 3600) / 60);
var sec = (second - hr * 3600 - min * 60);// equal to => var sec = second % 60;
dateStr = hr + ":" + min + ":" + sec;
return dateStr;
}
//目前有2個(gè)時(shí)鐘,影響性能,合并下去掉countdown,于是countdown4micro變成以下的樣子:
function countdown4micro(that) {
var loop_second = Math.floor(loop_index / 100);
// 得知經(jīng)歷了1s
if (cost_micro_second != loop_second) {
// 賦予新值
cost_micro_second = loop_second;
// 總秒數(shù)減1
total_second--;
}
// 每隔一秒,顯示值減1; 渲染倒計(jì)時(shí)時(shí)鐘
that.setData({
clock:dateformat(total_second - 1)
});
if (total_second == 0) {
that.setData({
// micro_second: "",
clock:"時(shí)間到"
});
clearTimeout(micro_timer);
return ;
}
if (micro_second_current <= 0) {
micro_second_current = micro_second_init;
}
micro_timer = setTimeout(function(){
that.setData({
micro_second: micro_second_current - 1
});
micro_second_current--;
// 放在最后++,不然時(shí)鐘停止時(shí)還有10毫秒剩余
loop_index ++;
countdown4micro(that);
}
,10)
}
如此這般,毫秒與時(shí)分秒是分別運(yùn)行渲染的,再次改造,程序可讀性更好。dateformat針對(duì)于毫秒操作,而不接受秒為數(shù)。同時(shí)還省卻了計(jì)算100次為1s的運(yùn)算
/**
* 需要一個(gè)目標(biāo)日期,初始化時(shí),先得出到當(dāng)前時(shí)間還有剩余多少秒
* 1.將秒數(shù)換成格式化輸出為XX天XX小時(shí)XX分鐘XX秒 XX
* 2.提供一個(gè)時(shí)鐘,每10ms運(yùn)行一次,渲染時(shí)鐘,再總ms數(shù)自減10
* 3.剩余的秒次為零時(shí),return,給出tips提示說(shuō),已經(jīng)截止
*/
// 定義一個(gè)總毫秒數(shù),以一分鐘為例。TODO,傳入一個(gè)時(shí)間點(diǎn),轉(zhuǎn)換成總毫秒數(shù)
var total_micro_second = 2 * 1000;
/* 毫秒級(jí)倒計(jì)時(shí) */
function countdown(that) {
// 渲染倒計(jì)時(shí)時(shí)鐘
that.setData({
clock:dateformat(total_micro_second)
});
if (total_micro_second <= 0) {
that.setData({
clock:"已經(jīng)截止"
});
// timeout則跳出遞歸
return ;
}
setTimeout(function(){
// 放在最后--
total_micro_second -= 10;
countdown(that);
}
,10)
}
// 時(shí)間格式化輸出,如3:25:19 86。每10ms都會(huì)調(diào)用一次
function dateformat(micro_second) {
// 秒數(shù)
var second = Math.floor(micro_second / 1000);
// 小時(shí)位
var hr = Math.floor(second / 3600);
// 分鐘位
var min = Math.floor((second - hr * 3600) / 60);
// 秒位
var sec = (second - hr * 3600 - min * 60);// equal to => var sec = second % 60;
// 毫秒位,保留2位
var micro_sec = Math.floor((micro_second % 1000) / 10);
return hr + ":" + min + ":" + sec + " " + micro_sec;
}
Page({
data: {
clock: ''
},
onLoad: function() {
countdown(this);
}
});
經(jīng)過(guò)如上優(yōu)化,代碼量減少一半,運(yùn)行效率也高了。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 微信小程序之發(fā)送短信倒計(jì)時(shí)功能
- 微信小程序 倒計(jì)時(shí)組件實(shí)現(xiàn)代碼
- 微信小程序動(dòng)態(tài)顯示項(xiàng)目倒計(jì)時(shí)效果
- 微信小程序注冊(cè)60s倒計(jì)時(shí)功能 使用JS實(shí)現(xiàn)注冊(cè)60s倒計(jì)時(shí)功能
- 微信小程序?qū)崿F(xiàn)倒計(jì)時(shí)60s獲取驗(yàn)證碼
- 微信小程序倒計(jì)時(shí)功能實(shí)現(xiàn)代碼
- 微信小程序倒計(jì)時(shí)功能實(shí)例代碼
- 微信小程序?qū)崿F(xiàn)團(tuán)購(gòu)或秒殺批量倒計(jì)時(shí)
- 小程序?qū)崿F(xiàn)短信登錄倒計(jì)時(shí)
相關(guān)文章
Android Studio時(shí)間選擇器的創(chuàng)建方法
這篇文章主要為大家詳細(xì)介紹了Android Studio時(shí)間選擇器的創(chuàng)建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android實(shí)現(xiàn)QQ新用戶注冊(cè)界面遇到問(wèn)題及解決方法
這篇文章主要介紹了Android實(shí)現(xiàn)QQ新用戶注冊(cè)界面遇到問(wèn)題及解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
使用 Swift 語(yǔ)言編寫 Android 應(yīng)用入門
為了能順利使用這份向?qū)?,你需要? 1. 可以編譯Swift源碼的Linux環(huán)境。stdlib目前只能在Linux環(huán)境下編譯成安卓可用版本。在嘗試為安卓構(gòu)建之前,確保你能夠參考Swift項(xiàng)目的README為L(zhǎng)inux做編譯。2016-04-04
Android使用animator實(shí)現(xiàn)fragment的3D翻轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了Android使用animator實(shí)現(xiàn)fragment的3D翻轉(zhuǎn)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)一對(duì)一藍(lán)牙聊天APP
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一對(duì)一藍(lán)牙聊天APP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06

