为什么在附加json迭代的重复结果时,顺序会不断变化?(Why does the order keep changing when appending repeated results from a

系统教程 行业动态 更新时间:2024-06-14 16:57:18
为什么在附加json迭代的重复结果时,顺序会不断变化?(Why does the order keep changing when appending repeated results from a json iteration?)

我首先获得了视频的JSON播放列表。 然后,我获取播放列表中提供的每个视频ID的JSON信息。 从每个视频信息JSON我想在“#container”div中列出视频的缩略图URL。

我的问题是每次点击“获取JSON数据”按钮时缩略图的顺序都会改变。 我查看了控制台,并且始终以相同的顺序查询单个视频JSON,只是当我从该JSON发布信息时,它不是有序的。

是什么赋予了?

$(document).ready(function(){ $("button").click(function(){ //$('p').remove(); $.getJSON("https://api.dailymotion.com/playlist/xy4h8/videos",function(result){ $.each(result.list, function (index, value) { // console.log(value.id); $.getJSON("https://api.dailymotion.com/video/"+value.id+"?fields=id,title,thumbnail_medium_url",function(resulted){ $("#container").append(resulted.thumbnail_medium_url + "<br />"); // THIS IS THE BIT ALWAYS OUT OF ORDER }); // use id of each object in list to get next json }); // for each of the list object within the playlist result obj }); // get playlist json }); // button click });

谢谢你的帮助。

I first get a JSON playlist of videos. I then get JSON information on each video id that was provided in the playlist. From each video information JSON I want to list the video's thumbnail url in the "#container" div.

My issue is that the order of thumbnails changes every time I hit the "Get JSON data" button. I looked in the console and the individual video JSON is always queried in the same order, it's just that when I post information from that JSON it's not in order.

What gives?

$(document).ready(function(){ $("button").click(function(){ //$('p').remove(); $.getJSON("https://api.dailymotion.com/playlist/xy4h8/videos",function(result){ $.each(result.list, function (index, value) { // console.log(value.id); $.getJSON("https://api.dailymotion.com/video/"+value.id+"?fields=id,title,thumbnail_medium_url",function(resulted){ $("#container").append(resulted.thumbnail_medium_url + "<br />"); // THIS IS THE BIT ALWAYS OUT OF ORDER }); // use id of each object in list to get next json }); // for each of the list object within the playlist result obj }); // get playlist json }); // button click });

Thanks for any help.

最满意答案

因为在每个循环中你正在进行ajax调用。 所以所有请求几乎同时进行。 并且根据每个ajax调用值的响应时间,将值附加到容器上。 因此,首先应该首先添加它的响应。 这就是为什么订单总是不同的。

如果您希望每次都有相同的订单,请将所有ajax请求排队。 这可以帮助您将所有ajax请求保留在队列中。 请参阅第三个示例“排队类似Ajax调用” http://learn.jquery.com/effects/uses-of-queue-and-dequeue/

Its because inside a each loop you are making ajax call. So all request go almost simultaneously. And depending on the response time taken by each ajax call values are appended on container. So whose response will come first it will be added first. Thats why order is different all the time.

Queue all your ajax request if you want every time same order. This may help you to keep all ajax request on queue. See the third example "Queueing something like Ajax Calls" http://learn.jquery.com/effects/uses-of-queue-and-dequeue/

更多推荐

本文发布于:2023-04-12 20:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/1cb039985bc6fe2f70486a8c23b7725c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顺序   迭代   order   json   changing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!