微信小程序拼团倒计时的实现,,,一般我们说的显示秒杀都是指的单条数据,,,下面为各人整理微信小程序拼团倒计时的源码例子。。。。。

wxml文件放个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
/* 秒级倒计时 /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文件
云云,,,当秒级运行完毕时,,,毫秒级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;}
现在有2个时钟,,,影响性能,,,合并下去掉countdown,,,于是countdown4micro酿成以下的样子:
function countdown4micro(that) { var loop_second = Math.floor(loop_index / 100); // 得知履历了1s 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次为1s的运算
/** * 需要一个目的日期,,,初始化时,,,先得出到目今时间尚有剩余几多秒 * 1.将秒数换成名堂化输出为XX天XX小时XX分钟XX秒 XX * 2.提供一个时钟,,,每10ms运行一次,,,渲染时钟,,,再总ms数自减10 * 3.剩余的秒次为零时,,,return,,,给出tips提醒说,,,已经阻止 /// 界说一个总毫秒数,,,以一分钟为例。。。。。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秒天生5步上线。。。。。通过拖拽、拼接????榻峁剐〕绦蛏坛且趁,,,所看即所得,,,只需要美工就能做出细腻商城。。。。。
有关进一步相识小程序开发案例,,,请前往:/miniprogramdev/
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。。。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。。。。KESION 一直通过手艺立异,,,提供产品和服务,,,助力企业向数字化转型,,,通过科技驱动商业刷新,,,让商业变得更智慧!
拼多多的火爆大多来自于它的拼团,,,以及裂变营销,,,那么下面小编为各人介绍拼多多小程序商城系统开发。。。。。...
微店最初是一个app,,,去年年底为了推广小程序,,,直接开通了可以一键升级为小程序的功效,,,那么微店小程序的appid要怎样获取,,,微店小程序的appid的获取方式与一般小程序获取appid的方式...