小程序-倒计时
这里有个坑:
api返回如果格式是: ' 2020-02-20 02:02:02 '格式,则ios系统的小程序展示生成倒计时为null,把 ' - ' 替换为 ’ / ‘ 才能正常显示倒计时; replace(/\-/g, '/')
countTdown: function (renderdataTime) { // 渲染倒计时间
let that = this;
let dataTime = renderdataTime;
clearInterval(that.data.activityInter);
that.data.activityInter = setInterval(() => {
dataTime--;
if (dataTime < 0) {
clearInterval(that.data.activityInter);
that.setData({
overTimeFlag: true
})
return;
}
var timeObj = that.forMatterTime(dataTime);
var day = timeObj.days;
var hour = timeObj.hours;
var minutes = timeObj.minutes;
var seconds = timeObj.seconds;
that.setData({
activityTime: {
day: day,
hour: hour,
minute: minutes,
second: seconds
},
})
}, 1000);
},
//获取时间
forMatterTime: function (leftTime) {
var days = parseInt(leftTime / 60 / 60 / 24 % 24, 10); //计算剩余的天数
var hours = parseInt(leftTime / 60 / 60 % 24, 10); //计算剩余的小时
var minutes = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟
var seconds = parseInt(leftTime % 60, 10);//计算剩余的秒数
hours = this.checkTime(hours);
minutes = this.checkTime(minutes);
seconds = this.checkTime(seconds);
return {
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
}
},
checkTime: function (i) { //将0-9的数字前面加上0,例1变为01
if (i < 10) {
i = "0" + i;
}
return i;
},