嗨,我在 JSON 中有一个循环,可以在触发错误之前重试连接 3 次,但有时我有 3-4 个 JSON 请求,并且所有这些请求都可能会出错,因此我的 PhoneGap 应用程序中有 3 个警报。
EG。
函数 showJSONerror(xhr, 状态) { if (xhr.status === 0 && localStorage["TmpAlertShow"] !== true) { navigator.notification.confirm('连接错误。 验证您的网络连接并重试。',confirmAction,'糟糕...','再试一次,关闭'); localStorage["TmpAlertShow"] = true; 函数确认操作(按钮){ if (button == 1) { localStorage["TmpAlertShow"] = false; showPage(localStorage["TmpWebView"]) } if (button == 2) { localStorage["TmpAlertShow"] = false;返回假; } } }
我正在尝试找到通过JS关闭先前警报的方法,或者如果警报已经触发但未关闭,则记录状态(防止显示多个警报)
谢谢
您可以尝试设置一个全局变量,该变量是当前正在运行的请求的计数,然后在错误函数中,假设计数是否大于 0,则将结果存储在全局数组中,如果不是,则处理错误以进行显示。
示例:
var callsRunning = 0;
var callStatuses = [];
运行呼叫时添加:
callsRunning++;
通话结束后:
callsRunning--;
在你的错误函数中:
if(callsRunning > 0) {
callStatuses.push(/*whatever you want to collect*/);
}
else {
/*compile statuses for display of error*/
}
我知道这已经很旧了,但(不)令人惊讶的是,从 Cordova 12.x、12/2023 开始,Android 有时仍然会这样做。我现在的解决方案只是设置一个 1 秒延迟的布尔值。我猜想可能有一种更巧妙的方法来处理,但到目前为止这似乎有效。
// boolean to prevent accidental/multiple doConfirm calls (Android quirk?)
var doConfirmJustRan = false;
function doConfirm(confirmText, confirmTitle, confirmCallback){
try{
/* doConfirm just ran - don't run again yet! */
if(doConfirmJustRan == true){
console.log('doConfirm() tried to run again/too soon [' + confirmText + ']. exiting now.');
return;
}
/* do main navigator.notification.confirm stuff here ... */
/* clear var in 1 second (?) */
doConfirmJustRan = true;
window.setTimeout(function(){doConfirmJustRan = false;},1000);
}catch(e){ /* ... */ }
}