计算时间差开始倒计时

// 声明
countDown = "00:00";
 
  
// 页面引用
  <p class="cp-main-tip">剩余时间{{countDown}}<button id="hand-btn" class="hand-btn">交卷</button></p>

 



timeFn(d1) {
var t = "2018-02-03T14:30:00Z"; setInterval(()=>{ var endTime = new Date(t); var ss = (endTime.getTime() - (new Date()).getTime()) / 1000 ; var hour = Math.floor(ss / 60 / 60); var mins = Math.floor((ss / 60 % 60)); var scds = Math.floor(ss % ( 60)); this.countDown = ''; if(hour > 0){ this.countDown += (hour < 10 ? ('0' + hour) : hour) + ':'; } this.countDown += (mins < 10 ? ('0' + mins) : mins) + ':'; this.countDown += (scds < 10 ? ('0' + scds) : scds); },1000) };

 

 

 

 

// // 倒计时
    timeFn() {
        const params = {
            'endTimeStr': this.endTime   // 03:03:25
        };
        this.dataService
            .CountDown(params)
            .then((data) => {
                let timeArray = data.endTime.split(':');
                this.hour = parseInt(timeArray[0]);
                this.mins = parseInt(timeArray[1]);
                this.scds = parseInt(timeArray[2]);
                let timeCountScd = this.hour * 60 * 60 + this.mins * 60 + this.scds;
              this.allTimeCount = setInterval(() => {
                    this.countDown = '';
                    // 时间为负值时跳出考试
                    if (timeCountScd  < 0) {
                        this.countDown = '考试时间已截止';
                        this._hiddenPaper = true;
                        // 跳转到列表页
                        clearInterval(this.allTimeCount);
                        setTimeout(() => {
                            this.router.navigate(['main/student']);
                        }, 10000);
                    }else {
                        timeCountScd -- ;
                        if ( timeCountScd === 0) {
                            this._message.create('success', `试卷已提交,考试结束!`, {nzDuration: 5000});
                            this.handlePage();
                            // 保存成功后跳转到列表页
                            clearInterval(this.allTimeCount);
                            setTimeout(() => {
                                this.router.navigate(['main/student']);
                            }, 5000);
                        }
                        this.hour = Math.floor(timeCountScd / 60 / 60);
                        this.mins = Math.floor(timeCountScd / 60 % 60);
                        this.scds = Math.floor( timeCountScd % 60);
                        this.countDown += ( this.hour < 10 ? ('0' + this.hour) : this.hour) + ':';
                        this.countDown += ( this.mins < 10 ? ('0' + this.mins) : this.mins) + ':';
                        this.countDown += ( this.scds < 10 ? ('0' + this.scds) : this.scds);
                    }

                }, 1000)

            });

    };

 

转载于:https://www.cnblogs.com/linsx/p/8410858.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值