gpt4 book ai didi

javascript - 关闭 AlertController 并停止函数调用

转载 作者:行者123 更新时间:2023-12-01 01:31:20 25 4
gpt4 key购买 nike

我有一个函数,它显示一个警报 Controller 和一个按钮,单击该按钮会更新 Firestore 数据库上的字段。 15 秒后,警报消失并触发另一个函数。

pickupIsRequested(){
this.pickupRequest.pickupStatus.subscribe(value => {
console.log(value);

if(value == true){
let alert = this.alertCtrl.create({
title: 'You have a Pickup Request!',
message: '<span>Princess who is 3 minutes away requests a ride</span><hr/> You have 15 seconds to accept the request!',
buttons: [
{
text: 'Accept Ride',
handler: () => {
this.driver.updateDriverInRide(true, this.driverId);
}
}
]
});
alert.present();

setTimeout(() => {
alert.dismiss();
this.driver.updateDriverPickupRequest(false, this.driverId);
}, 15000);
}
});
}

单击警报 Controller 按钮后,如何停止对 setTimeout 方法的 function 调用?现在,如果我单击按钮,this.driver.updateDriverPickupRequest 仍然会触发。

最佳答案

您可以使用clearTimeout()取消您的函数

pickupIsRequested(){
// First you need to create your timeout
let myTimeout = setTimeout(() => {
alert.dismiss();
this.driver.updateDriverPickupRequest(false, this.driverId);
}, 15000);

this.pickupRequest.pickupStatus.subscribe(value => {
console.log(value);

if(value == true)
{
let alert = this.alertCtrl.create({
title: 'You have a Pickup Request!',
message: '<span>Princess who is 3 minutes away requests a ride</span><hr/> You have 15 seconds to accept the request!',
buttons: [
{
text: 'Accept Ride',
handler: () => {
this.driver.updateDriverInRide(true, this.driverId);
// Cancel it here
clearTimeout(myTimeout);
}
}
]
});
alert.present();
}
});

}

关于javascript - 关闭 AlertController 并停止函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53248529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com