详解小程序毫秒级倒计时(适用于拼团秒杀功能)
程序员文章站
2022-06-09 10:35:08
废话不多说直接上代码:
效果图:
index.js
page({
/**
* 页面的初始数据
*/
data: {
cou...
废话不多说直接上代码:
效果图:
index.js
page({ /** * 页面的初始数据 */ data: { countdown:'' , enddate2: '2018-08-08 11:41:00' }, /** * 生命周期函数--监听页面加载 */ onload: function (options) { var that = this; that.counttime() }, counttime() { var that = this; var date = new date(); var now = date.gettime(); var enddate = new date(that.data.enddate2);//设置截止时间 var end = enddate.gettime(); var lefttime = end - now; //时间差 var d, h, m, s, ms; if (lefttime >= 0) { d = math.floor(lefttime / 1000 / 60 / 60 / 24); h = math.floor(lefttime / 1000 / 60 / 60 % 24); m = math.floor(lefttime / 1000 / 60 % 60); s = math.floor(lefttime / 1000 % 60); ms = math.floor(lefttime % 1000); ms = ms < 100 ? "0" + ms : ms s = s < 10 ? "0" + s : s m = m < 10 ? "0" + m : m h = h < 10 ? "0" + h : h that.setdata({ countdown: d + ":" + h + ":" + m + ":" + s + ":" + ms, }) //递归每秒调用counttime方法,显示动态时间效果 settimeout(that.counttime, 100); } else { console.log('已截止') that.setdata({ countdown:'00:00:00' }) } }, })
index.html
<view> <text>{{countdown}}</text> </view>
以上所述是小编给大家介绍的小程序毫秒级倒计时详解整合,希望对大家有所帮助