首页 > 解决方案 > 基于一定的时间间隔清除超时

问题描述

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

        }

标签: javascriptangularfirebase-authenticationionic3

解决方案


这是成功完全完成代码执行后的解决方案,您必须调用clearInterval()它可以通过调用 clearInterval() 方法来停止执行,clearInterval(interval)在您完成工作后必须清除的情况下,请参阅w3school更多的


推荐阅读