<view class="oa_btn_time"><text class="oa_list_grary">支付剩余:</text>{{item.countDownTime}} </view>
import utils from '../../utils/util'
data(){
list:[{endTime: "2022-08-24 11:44:37"}]
},
onLoad(options) {
this.getList()
},
getList() {
let that = this
that.data.list.forEach((item, index) => {
item.countDownTime = "";
that.countDown(index);
});
},
countDown(i) {
let that = this;
let item = that.data.list[i];
that.data.list[i].countDownFn = setInterval(() => {
if (utils.countDownFun(item.countDownTime) == "已超时") {
clearInterval(that.list[i].countDownFn); //清除定时器
} else {
item.countDownTime = utils.countDownFun(item.endTime);
that.setData({
list: that.data.list
})
utils.countDownFun(item.endTime)
}
}, 1000);
},
utils .js
/**
* 订单未付款 倒计时
* @param {*} time 结束时间
*/
function countDownFun(time) {
// console.log(time)
let startTime = new Date(); //当前时间
let end = new Date(time); //结束时间
// console.log(end)
let result = parseInt((end - startTime) / 1000); //计算出豪秒
let d = parseInt(result / (24 * 60 * 60)); //用总共的秒数除以1天的秒数
let h = parseInt((result / (60 * 60)) % 24); //精确小时,用去余
let m = parseInt((result / 60) % 60); //剩余分钟就是用1小时等于60分钟进行趣余
let s = parseInt(result % 60);
//统一格式的显示
d < 10 ? d = '0' + d : d;
h < 10 ? h = '0' + h : h;
m < 10 ? m = '0' + m : m;
s < 10 ? s = '0' + s : s;
//当倒计时结束时,改变内容
if (result <= 0) {
return "已超时";
}
if (d == '00') {
return h + "时" + m + "分" + s + "秒";;
}
if (d == '00' && h == '00') {
return m + "分" + s + "秒";;
}
if (d == '00' && h == '00' && m == '00') {
return s + "秒";;
}
return d + "天" + h + "时" + m + "分" + s + "秒";
}
module.exports = {
countDownFun
}