【问题标题】:Cordova fails in checking appAvailability inside loop.How can i make loop wait until appAvailability.check execute?Cordova 在循环内检查 appAvailability 失败。如何让循环等到 appAvailability.check 执行?
【发布时间】:2015-07-31 11:49:02
【问题描述】:

这是从我的网站加载应用程序名称的 getapps 函数。

getapps = function (applist){   
var xmlhttp = new XMLHttpRequest();
var url = "http://mywebsite.com/"+applist;
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var myArr = JSON.parse(xmlhttp.responseText);
        myFunction(myArr);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

    function myFunction(data) {
    var i;
    var query = data.data;
        if(query != ''){
            for(i = 0; i < query.length; i++) {
                var appinstall='';
                appAvailability.check(
                    query[i].appid,       // URI Scheme or Package Name
                    function() {  // Success callback
                         appinstall = "is available :)";
                        console.log(query[i].appid+"is available :)");
                    },
                    function() {  // Error callback
                        appinstall = "is not available :(";
                        console.log(query[i].appid+"is not available :(");
                    }
                );
                console.log(appinstall);

            }
        }
    }

}

console.log 位于 appAvailability.check 之外的函数首先触发 n 次 在接下来的几秒钟内 console.log 位于 appAvailability.check 内部的函数会触发 n 次,但出现错误 undefined appid。

我通过删除 for 循环和预定义 appid 进行了测试,它运行良好且没有错误。

如何通过让循环等待appAvailability.check 完成来解决这个问题?

【问题讨论】:

    标签: javascript jquery cordova phonegap-plugins cordova-plugins


    【解决方案1】:

    这是因为appAvailability.check 执行了成功和失败的回调。

    我强烈怀疑您的代码正在该回调中进行本机调用,该回调在您的回调执行后运行该回调。

    您可以使用递归解决此问题,如下所示:

    function getApps(appslist) {
        var xmlhttp = new XMLHttpRequest(),
            url = "http://mywebsite.com/"+applist;
    
        callback = callback || function() {};
    
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var data = JSON.parse(xmlhttp.responseText);
    
                if (data != ''){
                    checkApp(data.data);
                }
            }
        }
    
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }
    
    function checkApp(applist, callback) {
        var app = applist.push();
    
        if (!app) {
            callback();
            return;
        }
    
        appAvailability.check(
            app.appid,      // URI Scheme or Package Name
            function() {    // Success callback
                 console.log(app.appid + "is available");
    
                 // handle availability here
    
                 checkApp(applist, callback);
            },
            function() {  // Error callback
                console.log(app.appid + "is not available");
    
                // handle absence here
    
                checkApp(applist, callback);
            }
        );
    }
    

    【讨论】:

    • 检查我的更新 - 基本上使用递归从回调中调用函数
    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2020-11-27
    • 2013-01-04
    • 2013-11-20
    相关资源
    最近更新 更多