好的,我在下面有这个函数,我每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秒继续执行一次,因为它不会离开页面。
如果出现问题,我怎样才能让它只显示一次警报?
答案 0 :(得分:2)
保留对间隔的引用,并在间隔失败时将其清除。
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.');
});
答案 1 :(得分:0)
不确定这是否是最好的方法,但是动态创建dom元素并检查其存在是否有效,您甚至可以检查其值并更改它们以执行任何其他操作;所以基本上它作为一个标志,如果需要可以作为其他事件触发的条件检查器:
.fail(function() {
// create a hidden element in dom to check for the alert event execution
if ($('#errorchecker').length === 0) {
$('body').append('<input type="hidden" id="errorchecker" value="executed" />');
alert('There seems to have been a problem with continuing the verification process; please contact us if this continues to occur.');
}
});