// 声明
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) }); };