效果图
.wxml
<view class="TimeSeond">{{second}}</view>
.js
Page({
data: {
tiem_one:'',
second:'',//倒计时
deadline:'',
},
onLoad(){
this.countdown();
},
countdown(){
let timestamp = Date.parse(new Date()) / 1000;//当前时间戳
let time = this.addtime('2024-4-19 17:20:00');//当前时间转时间戳
if(time > timestamp){
this.deadline = (time - timestamp) * 1000
this.setData({
deadline:this.deadline
})
this.single()
}else{
that.setData({
second:'00:00'
})
}
},
addtime(time){
let date = new Date(time);
let minutes = date.getMinutes();
date.setMinutes(minutes+30);//增加三十分钟
return Date.parse(date) / 1000;
},
single(e){
var that = this
var time = that.deadline,minutes,seconds;
that.tiem_one = setInterval(function() {
time = time - 1000
minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60));
seconds = parseInt((time % (1000 * 60)) / 1000);
if (time < 1) {
that.setData({
second:'00:00'
})
clearInterval(that.tiem_one)
return false
}
minutes = minutes > 9 ? minutes : '0' + minutes
seconds = seconds > 9 ? seconds : '0' + seconds
that.setData({
second:minutes + ':' + seconds
})
},1000)
},
})
遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。