/**
* 生命周期函数--监听页面加载
*/
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, 1000);
} else {
console.log('活动已经结束')
that.setData({
countdown:'00:00:00'
})
}
}
以上就是关于在网站制作中遇到的如何使用js代码来实现倒计时功能的方法,大家可以参考以下