jquery - Why does the order keep changing when appending repeated results from a json iteration? -
i first json playlist of videos. json information on each video id provided in playlist. each video information json want list video's thumbnail url in "#container" div.
my issue order of thumbnails changes every time hit "get json data" button. looked in console , individual video json queried in same order, it's when post information 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 />"); // bit out of order }); // use id of each object in list next json }); // each of list object within playlist result obj }); // playlist json }); // button click }); thanks help.
its because inside each loop making ajax call. request go simultaneously. , depending on response time taken each ajax call values appended on container. response come first added first. thats why order different time.
queue ajax request if want every time same order. may keep ajax request on queue. see third example "queueing ajax calls" http://learn.jquery.com/effects/uses-of-queue-and-dequeue/
Comments
Post a Comment