小程序JavaScript倒计时无法每秒自动刷新的问题求助
微信小程序倒计时器无法自动刷新动态效果的问题
开发微信小程序时,使用JavaScript编写的倒计时器无法每秒自动刷新,无法呈现出计时器应有的动态效果,尝试修复但未能解决。以下是我的JavaScript代码:
// Event end date list data let goodsList = [ {actEndTime: '2023-1-22 10:00:43'}, ] Page({ data: { countDownList: [], actEndTimeList: [] }, onLoad(){ let endTimeList = []; // Commit the end time parameters of the event into a separate array for easy operation goodsList.forEach(o => {endTimeList.push(o.actEndTime)}) this.setData({ actEndTimeList: endTimeList}); // Execute countdown function this.countDown(); }, timeFormat(param){// Formatting function less than 10 return param <10? '0' + param: param; }, countDown(){// Countdown function // Get the current time and the end time array of the event let newTime = new Date().getTime(); let endTimeList = this.data.actEndTimeList; let countDownArr = []; // Process the end time and render it to the page endTimeList.forEach(o => { let endTime = new Date(o).getTime(); let obj = null; // If the event is not over, process the time if (endTime-newTime> 0){ let time = (endTime-newTime)/1000; // Get day, hour, minute, second let day = parseInt(time/(60 * 60 * 24)); let hou = parseInt(time% (60 * 60 * 24)/3600); let min = parseInt(time% (60 * 60 * 24)% 3600/60); let sec = parseInt(time% (60 * 60 * 24)% 3600% 60); obj = { day: this.timeFormat(day), hou: this.timeFormat(hou), min: this.timeFormat(min), sec: this.timeFormat(sec) } }else{// The event has ended, all set to '00' obj = { day: '00', hou: '00', min: '00', sec: '00' } } countDownArr.push(obj); }) // Render, and then execute a countdown function every second this.setData({ countDownList: countDownArr}) setTimeout(this.countDown,1000); } })
补充效果说明
重启项目编译后的效果如下:
注:使用JavaScript开发该小程序。
内容的提问来源于stack exchange,提问作者aufa
相关产品推荐
相关产品推荐

