【问题标题】:navigator.notification.confirm() - firing multiple timesnavigator.notification.confirm() - 多次触发
【发布时间】:2014-02-04 08:02:10
【问题描述】:

您好,我在 JSON 中有一个循环,可在触发错误之前重试连接 3 次,但有时我有 3-4 个 JSON 请求,所有请求都可能出现错误,因此我的 phonegap 应用程序中有 3 个警报。

EG。

函数 showJSONerror(xhr, 状态) { if (xhr.status === 0 && localStorage["TmpAlertShow"] !== true) { navigator.notification.confirm('连接错误。\n 验证您的网络连接并再试一次。', confirmAction, '糟糕...','再试一次,关闭'); localStorage["TmpAlertShow"] = true; 功能确认操作(按钮){ if (button == 1) { localStorage["TmpAlertShow"] = false; showPage(localStorage["TmpWebView"]) } if (button == 2) { localStorage["TmpAlertShow"] = false;返回假; } } }

我正在尝试找到通过 JS 关闭先前警报的方法,或者如果警报已触发且未关闭,则记录状态(防止显示多个警报)

谢谢

【问题讨论】:

    标签: javascript json cordova


    【解决方案1】:

    您可以尝试设置一个全局变量,该变量是当前正在运行的请求的计数,然后在您的错误函数中,例如如果计数大于 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*/
    }
    

    【讨论】:

    • 所以如果我理解正确的话,这就是如何做到的? code var callsRunning = 0; function showJSONerror(xhr, status) { if (xhr.status === 0 && callsRunning > 0) { navigator.notification.confirm('Connection error.\n Verify your network connection and try again.', confirmAction, 'Woops...','Try Again, Close'); callsRunning++; function confirmAction(button) { if (button == 1) { callsRunning--; showPage(localStorage["TmpWebView"]) } if (button == 2) { callsRunning--; return false; } } }
    • 看来我可能没有理解这个问题。对不起
    【解决方案2】:

    我之前也遇到过类似的问题

    我曾经解决过http://underscorejs.org/#after

    也许可以试一试?

    【讨论】:

    • 是的,但这意味着我必须在顶部使用额外的库来解决这个问题:/
    • 如果这是您唯一需要的功能,您可以模仿相同的代码吗?
    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多