發表意見之後...使用$.when.apply()
辦法看到更新的答案。
document.addEventListener("intel.xdk.device.barcode.scan", function(evt){
if (evt.success == true) {
//successful scan
var barcode = evt.codedata;
//Get numbers of Orders
$.getJSON("http://www.testurl.de/api/orders/?ws_key=TEEUQWL9J76LFQE&output_format=JSON", function(data){
var order = JSON.stringify(data);
var orderdata =JSON.parse(order);
var ordercount = Object.keys(orderdata.orders).length;
var queries = [];
var result = 0;
var i = 0;
while(i <= ordercount)
{
queries.push(
$.getJSON("http://www.testurl.de/api/orders/"+i+"?ws_key=TEZQFW41L9J76LFQE&output_format=JSON", function(data){
var shopdata = JSON.stringify(data);
var myshopdata =JSON.parse(shopdata);
result++; // Increment results counter
sweetAlert("Code will be checkd", "Please wait","info");
if(myshopdata.order.reference == barcode)
addcode(myshopdata.order.reference,myshopdata.order.associations.order_rows[0].product_name,myshopdata.order.total_paid_tax_incl,myshopdata.order.associations.order_rows[0].product_id);
});
);
i++;
} //end of while loop
// When all the getJSON calls have completed
$.when.apply($, queries).done(function() {
if(!result) // check results
console.log('No orders found');
});
}); //end of get json
}//end of event listener if
else
sweetAlert("Hoppla", "Woltest du nicht gerade scannen? :) ");
},false);//end of event listener
'$ .getJSON is async' ...所以你的代碼行爲正常。你需要添加'success'和'error'回調來管理這個。 – AdamJeffers
是的我知道,但我不知道我可以如何使用它在我的示例代碼:( – Devil089
從這裏檢查http://api.jquery.com/jquery.getjson/ –