gpt4 book ai didi

javascript - 如何阻止通过 AJAX 重复调用某个函数时多次显示警报?

转载 作者:行者123 更新时间:2023-12-02 17:26:30 25 4
gpt4 key购买 nike

好的,我在下面有这个函数,我每 5 秒调用一次..

function checkVerfToken(user_id) {

// Set file to get results from..
var loadUrl = "ajax_files/check_verf_token.php";

// Set parameters
var dataObject = { user_id: user_id };

// Run request

getAjaxData(loadUrl, dataObject, 'POST', 'html')

.done(function(response) {

if (response == 'success') {
// Reload the page
window.location.replace('payment_completed_verf.php');
}

})

.fail(function() {
alert('There seems to have been a problem with continuing the verification process; please contact us if this continues to occur.');
});

// End

}

我每 5 秒从一个页面调用它:

<script type="text/javascript">
window.setInterval(function(){
checkVerfToken(<?=$user_id?>);
}, 5000);
</script>

但是,如果执行 fail ,那么它显然会每 5 秒执行一次,因为它不会离开页面。

如果出现问题,如何让它只显示一次警报?

最佳答案

保留对间隔的引用,并在失败时清除它。

verifyInterval = window.setInterval(function(){
checkVerfToken(<?=$user_id?>);
}, 5000);

.fail(function() {
clearInterval(verifyInterval);
alert('There seems to have been a problem with continuing the verification process; please contact us if this continues to occur.');
});

关于javascript - 如何阻止通过 AJAX 重复调用某个函数时多次显示警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455844/

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