我正在计时器。我有一个保存秒值的变量。如果我使用onclick()手动从变量中减去一个,它将起作用。但是,当我通过setInterval运行该函数时,它将返回NaN。
我尝试过在更改值时在变量前添加“ +”,还尝试了Number()函数,但无济于事。
export class CountdownPage implements OnInit {
countdown: any;
tabata: any;
id: any;
set: any;
currentSet: any;
setTest: any;
phaseCountdown: any;
currentPhase: any;
phaseWork: any;
phaseRest: any;
phaseEnd: any;
intervalOne: any;
authState$: Observable<boolean>;
constructor(private Auth: AuthService, public http: HttpClient, private
router: Router, private route: ActivatedRoute) {
this.route.queryParams.subscribe(params => {
if (this.router.getCurrentNavigation().extras.state) {
this.countdown =
this.router.getCurrentNavigation().extras.state.tabataData[0].substring(17, 19);
this.tabata = this.router.getCurrentNavigation().extras.state.tabataData[1];
this.id = this.router.getCurrentNavigation().extras.state.tabataData[2];
console.log(this.countdown);
}
});
}
repeat() {
this.intervalOne = setInterval(this.begin, 1000);
}
begin() {
let first = document.querySelector('#counter');
this.countdown = this.countdown - 1;
Number(this.countdown);
first.innerText = this.countdown;
console.log(this.countdown);
}
有关信息:this.countdown变量是通过服务传递的(我正在使用Ionic AngularJS)。
我只是想将存储在变量中的数字每秒减少一个值。
谢谢。