我正在编写一个代码来检查是否在1分钟内输入了验证码,否则它将重定向到验证页面,否则它将继续进行注册过程。我能够在一分钟后重定向到身份验证,但是当用户键入验证并继续注册过程时,它会在一分钟后将用户重定向到身份验证页面,这意味着它不会清除超时。我该如何清除它?
display: string = "display";
firstAnimation: string = "first";
secondAnimation: string = "second";
thirdAnimation: string = "third";
fifthAnimation: string = "fifth";
bottomStyle: string = "bottom";
msgNumber: any;
token: any;
userid: any;
isSet: Boolean = true;
timers:any;
constructor(...) {
....
var count = 0;
var that = this
var timer = function () {
let promise = new Promise((resolve, reject) => {
if (count === 1) {
that.firstAnimation = "first-para-animation";
} else if (count === 2) {
that.firstAnimation = "first-second-fire- animation";
that.secondAnimation += " load-animation";
} else if (count === 3) {
// that.truthy = true;
that.bottomStyle = "no-margin"
that.fifthAnimation += " load-animation";
}
// check the length of count before pushing
if (count < messages.length) {
count += 1
} else {
clearInterval(interval)
}
if (count === 1) {
resolve()
}
return promise;
})
promise.then(() => {
if (that.isSet) {
this.timers = setTimeout(function () {
var counter = 0;
console.log(count, 'count')
if (count == 3 && counter <= 3) {
that.redirect();
}
counter += count;
}, 60000);
}else{
clearTimeout(this.timers);
}
})
}
// setting the interval after how long it should call the timer function
var interval = setInterval(timer, 1500)
}
redirect(){
console.log('Fired yes!!!');
let promise = new Promise((resolve, reject) => {
this.toastCtrl.create({
message: "Ooops, took time verifing your code, try again.",
duration: 5000,
position: 'bottom'
}).present();
resolve()
}).then(()=>{
this.navCtrl.setRoot(AuthDevicePage);
})
return promise;
}
verify() {
let signInCredential = firebase.auth.PhoneAuthProvider.credential(this.verification_id, this.code);
firebase.auth().signInWithCredential(signInCredential).then((user) => {
this.app.loadUser().then(success => {
this.isSet = false;
....
}
答案 0 :(得分:0)
这里有一个解决方案,在成功完成你的代码执行后你必须调用 clearInterval()它可以通过调用clearInterval()方法来停止执行
在您的情况下,您必须在完成工作后清除clearInterval(interval)
参考w3school for more