Javascript 基于特定间隔的清除超时

Javascript 基于特定间隔的清除超时,javascript,angular,firebase-authentication,ionic3,Javascript,Angular,Firebase Authentication,Ionic3,我正在写一个代码来检查是否在1分钟内输入了验证代码,如果没有,它将重定向到身份验证页面,否则它将继续注册过程。我可以在一分钟后重定向到身份验证,但当用户输入验证并继续注册过程时,它会在一分钟后将用户重定向到身份验证页面,这意味着它不会清除超时。我怎样才能把它弄清楚 display: string = "display"; firstAnimation: string = "first"; secondAnimation: string = "second"; th

我正在写一个代码来检查是否在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;
                    ....

        }
display:string=“display”;
firstAnimation:string=“first”;
secondAnimation:string=“second”;
第三信息:string=“第三”;
fifthAnimation:string=“第五”;
bottomStyle:string=“bottom”;
msgNumber:任何;
代币:任何;
userid:any;
isSet:Boolean=true;
计时器:任何;
构造函数(…){
....    
var计数=0;
var=this
变量计时器=函数(){
让承诺=新承诺((解决、拒绝)=>{
如果(计数==1){
that.firstAnimation=“第一段动画”;
}否则如果(计数==2){
that.firstAnimation=“first second fire-animation”;
that.secondAnimation+=“加载动画”;
}否则如果(计数==3){
//那是真的;
that.bottomStyle=“无保证金”
that.fifthAnimation+=“加载动画”;
}
//推前检查计数长度
如果(计数<消息长度){
计数+=1
}否则{
清除间隔(间隔)
}
如果(计数==1){
解决()
}
回报承诺;
})
答应。然后(()=>{
如果(那是我的错){
this.timers=setTimeout(函数(){
var计数器=0;
console.log(count,'count')
如果(计数==3&&计数器{
此文件为.toastCtrl.create({
消息:“oops,花时间验证您的代码,请重试。”,
持续时间:5000,
位置:'底部'
}).present();
解决()
}).然后(()=>{
this.navCtrl.setRoot(AuthDevicePage);
}) 
回报承诺;
}
验证(){
让signInCredential=firebase.auth.PhoneAuthProvider.credential(this.verification\u id,this.code);
firebase.auth().signInWithCredential(signInCredential)。然后((用户)=>{
this.app.loadUser().then(成功=>{
this.isSet=false;
....
}

这里有一个解决方案,成功完成代码执行后,您必须调用clearInterval()它将能够通过调用clearInterval()方法停止执行 在您的情况下,您必须在完成工作后清除
clearInterval(interval)
提及