购物车倒计时(angular 7天倒计时)

1,789 阅读1分钟

.html

<div *ngIf="data.OrderStatusId == 10">
    <p>还剩{{data.times}}订单自动确认完成</p>
</div>

.ts

 // 支付倒计时
    timediff(secondAll) {
        const day = Math.floor(secondAll / (60 * 60 * 24)); // 天数
        const hour = Math.floor((secondAll - day * 60 * 60 * 24) / (60 * 60)); // 小时数
        const min = Math.floor((secondAll - day * 60 * 60 * 24 - hour * 60 * 60) / 60); // 分钟
        let timeString = '';
        if (day > 0) {
            timeString = day + '天';
        }
        return timeString + hour + ':' + min;
    }
    //判断后端是否返回数据
     if (res.Data.length > 0) {
                res.Data.forEach(item => {//循环数据
                    const _time = new Date(item.CreatedOn).getTime();//获取订单数据的时间并转换成时间戳
                    const newTime = new Date().getTime();//获取当前时间并转换成时间戳
                    let curTime = 7 * 24 * 3600 - Math.floor((newTime - _time) / 1000); // 当前时间与订单时间 时间差
                    const _this = this;
                    if (curTime > 0) {
                        const timer = setInterval(function () {//定时器
                            curTime = curTime - 1;
                            if (curTime === 0) {
                                console.log('倒计时取消');
                                clearInterval(timer);//取消定时器
                            } else {
                                item.times = _this.timediff(curTime);//给对象添加times
                            }
                        }, 1000);
                    } else {
                    }
                });
            }