Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

微信小程序倒計時實作:從秒級到毫秒級的三次優化

編輯:關於 Android 程式設計

微信小程序電商常見的倒計時實作

wxml 先放一個 text

<text>second: {{second}} micro second:{{micro_second}}</text>

在 js 檔中呼叫:

 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); } }); 

先跑一次驗證:從數字遞減到 1 秒後顯示時間到,接著再把毫秒補上去。注意毫秒的步長受限於系統的計時頻率,因此精度取到 0.01 秒(10ms)較合適。

js

 /* 秒級倒計時 */ 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) } /* 毫秒級倒計時 */ // 初始毫秒數,同時作為歸零基準 var micro_second_init = 100; // 當前毫秒數 var micro_second_current = micro_second_init; // 毫秒計時器 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 >second: {{second}}s</text> <text>{{micro_second}}</text> 

如此一來,當秒級計時結束時,毫秒級 timer 也會被 clearTimeout,文字顯示為「micro_second too」。

接著再加一個 countdown4micro,讓畫面能顯示「剩余 0:3:19 89」這種格式的倒數。

 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; } // 目前有兩個計時器,影響效能,合併後去掉 countdown,countdown4micro 改成: function countdown4micro(that) { var loop_second = Math.floor(loop_index / 100); // 得知已經過了 1 秒 if (cost_micro_second != loop_second) { // 賦予新值 cost_micro_second = loop_second; // 總秒數減 1 total_second--; } // 每隔一秒,顯示值減 1; 渲染倒計時時鐘 that.setData({ clock:dateformat(total_second - 1) }); if (total_second == 0) { that.setData({ // micro_second: "", clock:"時間到" }); 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--; // 放在最後 ++,否則時鐘停止時還剩 10 毫秒 loop_index ++; countdown4micro(that); } ,10) } 

這樣毫秒與時分秒分別獨立渲染,可讀性更好。dateformat 改成接受毫秒參數,同時省掉了每 100 次換算 1 秒的運算。

 /** * 需要一個目標日期,初始化時先算出到當前時間还剩多少秒 * 1. 將秒數格式化輸出為 XX天XX小時XX分鐘XX秒 XX * 2. 提供一個時鐘,每 10ms 執行一次,渲染後把總毫秒數減 10 * 3. 剩餘毫秒為零時 return,並給出提示說已截止 */ // 定義一個總毫秒數,以一分鐘為例。TODO:傳入一個時間點,轉換為總毫秒數 var total_micro_second = 2 * 1000; /* 毫秒級倒計時 */ function countdown(that) { // 渲染倒計時時鐘 that.setData({ clock:dateformat(total_micro_second) }); if (total_micro_second <= 0) { that.setData({ clock:"已經截止" }); // timeout 則跳出遞歸 return ; } setTimeout(function(){ // 放在最後 -- total_micro_second -= 10; countdown(that); } ,10) } // 時間格式化輸出,如 3:25:19 86。每 10ms 都會被呼叫一次 function dateformat(micro_second) { // 秒數 var second = Math.floor(micro_second / 1000); // 小時位 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); } }); 

經過這幾輪優化,程式碼量減少一半,執行效率也更高。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved