【问题标题】:Clearing time out based on certain interval基于一定的时间间隔清除超时
【发布时间】:2018-11-23 09:08:15
【问题描述】:

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

        }

【问题讨论】:

    标签: javascript angular firebase-authentication ionic3


    【解决方案1】:

    这是一个解决方案,在成功完成代码执行后,您必须调用 clearInterval() 它将能够通过调用 clearInterval() 方法来停止执行 在您的情况下,您必须在完成工作后清除 clearInterval(interval) 参考w3school for more

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多