javascript - 基于一定的时间间隔清除超时
问题描述
我正在写一个代码来检查是否在 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;
....
}
解决方案
这是成功完全完成代码执行后的解决方案,您必须调用clearInterval()它可以通过调用 clearInterval() 方法来停止执行,clearInterval(interval)
在您完成工作后必须清除的情况下,请参阅w3school更多的
推荐阅读
- nginx - nginx + 上游并通过 url 中的 arg 切换上游
- python - 使用相应的字符串名称调用类
- javascript - 如何在最新的 Electron 版本中使用 Zerorpc
- angular6 - dist 下的符号链接文件夹内容未在 Angular 6 中呈现
- macos - Protege 5.5.0:在 Mac 上自动完成属性和个人的名称
- google-cloud-platform - 如何顺序调用云工作流(gcp)?
- java - 使用Java在MinIO中上传文件时如何进行服务器端加密
- odoo-11 - 在 Odoo 11 中覆盖 http.py 方法
- databricks - Databricks:如何解决 DBFS DOWN?
- php - 在 WAMP 中更新 PHP 版本后调用未定义的函数 curl_init()