1.一般用于活动的倒计时
1.先定义一个方法:
set_countDown() {
let now = new Date();
let datatime = this.datas.end_time.replace(/\-/g, '/'); // replace + 正则 匹配替换 / 斜杠
let endDate = new Date(datatime);
let leftTime = endDate.getTime() - now.getTime(); //计算剩余的毫秒数
if (leftTime <= 0) {
leftTime = 0;
}
let leftsecond = parseInt(leftTime / 1000); //计算剩余的秒数
let day = Math.floor(leftsecond / (60 * 60 * 24));
let hour = Math.floor((leftsecond - day * 24 * 60 * 60) / 3600);
let minute = Math.floor((leftsecond - day * 24 * 60 * 60 - hour * 3600) / 60);
let second = Math.floor(leftTime / 1000 % 60, 10);
hour = hour < 10 ? '0' + hour : hour
minute = minute < 10 ? '0' + minute : minute
second = second < 10 ? '0' + second : second
this.time = {
day: day,
hour: hour,
minute: minute,
second: second
}
},
2.从后台服务器拿到结束时间:
get_datas() {
var that = this
uni.$u.http.get('接口').then(res => {
that.datas = res.data
}).catch(err => {
})
},
3.在onshow 或者在 onlode中使用定时器一秒执行一次
onShow() {
setInterval(() => {
this.set_countDown()
}, 1000)
},
版权声明:本文为weixin_51391923原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。