wxml文件放個(gè)text
second: {{second}} micro second:{{micro_second}}
在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);
|