微信小程序倒计时组件
本课程的源码请移步文章末尾 先来看下最终效果: git源: http://git.oschina.net/dotton/CountDown 分步骤-性子急的朋友,可以直接看最后那段代码。 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); } }); 运行验证下,从10走到1s,然后显示时间到。 于是继续将毫秒完善,注意毫秒的步长受限于系统的时间频率,于是我们精确到0.01s即10ms js /* 秒级倒计时 */