我的getJSON调用随机返回结果,而不是它们被调用的顺序 [英] My getJSON calls are returning results randomly rather than the order they are called

查看:78
本文介绍了我的getJSON调用随机返回结果,而不是它们被调用的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要进行多个getJSON请求,以将数据写入HTML文件.此示例显示了3个请求,但我将以相同的格式添加多达6个(可能更多)请求.

I need to make multiple getJSON requests to write data to an HTML file. This example shows 3 requests yet I will add up to 6 requests (maybe more) in the same format.

我的问题是结果以随机顺序返回,而不是按照调用顺序返回.我怀疑它们是按照请求完成的顺序退还的.

My problem is that the results are returned in a random order rather in the order they are called. I suspect they are being returned in the order of the request's completion.

我每次必须以相同的顺序编写div.

I must write the divs in the same order every time.

这些值是从getJSON请求返回的,另外,在每个请求的情况下,还有两个实例来自每个请求唯一的本地变量集(nwsltrID[0]nwsltrNames[0]}.由于这两个变量(如代码示例所示),附加变为变量.

The values are returned from the getJSON request plus two instances in each request's case come from a local set of vars (nwsltrID[0] and nwsltrNames[0]} which are unique to each request. So, the HTML I'm appending becomes variable because of those two vars (shown in code example).

代码示例:

$.getJSON(url0, function (data) {
utcday =  data[0].createdOn;
ltrDay = moment.utc(utcday).format("DD MMM YY");                           

$('#listDiv').append("<div class= \"ellipsis\" ><div class=\"ltrFolder\" ><a title= \"Archive\" href = \"" + idString3 + nwsltrID[0] + "\"><i class=\"fa fa-folder-o\"></a></i></div><div id=\"" + data[0].id + "\"class= \"ellipsis listTitle\" >" + nwsltrNames[0] + "<br><a title= \"" + data[0].conversation + "\" class = \"addressClick\" id =\"" + url0 + "\"><span class =\"point\" ><i class=\"fa fa-newspaper-o\"></i></span>" + data[0].conversation + "</a><div class=\"ltgrey\" style=\"text-transform:uppercase;padding-top:3px;\">" + ltrDay + "</div></div><div style=\"padding: 0 20px 20px 0; color:#666666;\">" + data[0].textHead + "</div>");   
});

$.getJSON(url1, function (data) {

utcday =  data[0].createdOn;
ltrDay = moment.utc(utcday).format("DD MMM YY");                           

$('#listDiv').append("<div class= \"ellipsis\" ><div class=\"ltrFolder\" ><a title= \"Archive\" href = \"" + idString3 + nwsltrID[1] + "\"><i class=\"fa fa-folder-o\"></a></i></div><div id=\"" + data[0].id + "\"class= \"ellipsis listTitle\" >" + nwsltrNames[1] + "<br><a title= \"" + data[0].conversation + "\" class = \"addressClick\" id =\"" + url1 + "\"><span class =\"point\" ><i class=\"fa fa-newspaper-o\"></i></span>" + data[0].conversation + "</a><div class=\"ltgrey\" style=\"text-transform:uppercase;padding-top:3px;\">" + ltrDay + "</div></div><div style=\"padding: 0 20px 20px 0; color:#666666;\">" + data[0].textHead + "</div>"); 
});

$.getJSON(url2, function (data) {
utcday =  data[0].createdOn;
ltrDay = moment.utc(utcday).format("DD MMM YY");                           

$('#listDiv').append("<div class= \"ellipsis\" ><div class=\"ltrFolder\" ><a title= \"Archive\" href = \"" + idString3 + nwsltrID[2] + "\"><i class=\"fa fa-folder-o\"></a></i></div><div id=\"" + data[0].id + "\"class= \"ellipsis listTitle\" >" + nwsltrNames[2] + "<br><a title= \"" + data[0].conversation + "\" class = \"addressClick\" id =\"" + url2 + "\"><span class =\"point\" ><i class=\"fa fa-newspaper-o\"></i></span>" + data[0].conversation + "</a><div class=\"ltgrey\" style=\"text-transform:uppercase;padding-top:3px;\">" + ltrDay + "</div></div><div style=\"padding: 0 20px 20px 0; color:#666666;\">" + data[0].textHead + "</div>"); 
}); 

我敢肯定,我正在这里做一些其他的新手工作,可以改进和优化它们. 所以,我很想听听所有建议.

I'm certain I'm doing some other newbie stuff in here that can be improved and optimized. So, I'm eager to hear all suggestions.

我希望我提供了足够的信息来解决此问题.

I hope I have provided enough info to get this fixed.

推荐答案

要订购AJAX查询,您可以使用 $.when 函数-

To order AJAX queries you can use $.when function - http://api.jquery.com/jquery.when/:

以下是您的示例:

$.when( $.getJSON(url0), $.getJSON(url1), $.getJSON(url2) ).done( function() {
    $.each(arguments, function(index, result) {
        var data = result[0];
        utcday =  data[0].createdOn;
        ltrDay = moment.utc(utcday).format("DD MMM YY");

        $('#listDiv').append(... + nwsltrID[index] + .... + nwsltrNames[index]  + ... );
    });
});

无论您有多少查询-只需将其添加为$ .when函数的参数即可!

No matter how much queries you have - just add it as arguments for $.when function!

这篇关于我的getJSON调用随机返回结果,而不是它们被调用的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆