效果图
.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) * 1000this.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 = thisvar time = that.deadline,minutes,seconds;that.tiem_one = setInterval(function() {time = time - 1000minutes = 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' + minutesseconds = seconds > 9 ? seconds : '0' + secondsthat.setData({second:minutes + ':' + seconds})},1000)},
})
遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。