jquery - getJSON running order -



jquery - getJSON running order -

i'm aware getjson runs asynchronously i'm having problem understanding why programme returning same data. i'm looping through list of possible sites query , using getjson homecoming info manipulating info on each iteration of loop info i'm getting seems same. when running code straight expected value (not in loop). there have been odd occasions when i've received sec or 3rd set of info i'm assuming caused network beingness faster usual.

var songobj; for(i = 0; < networks.length; i++) { runjson(networks[i],"track",false); songobj = []; $.when.apply($, deferreds).then(function() { songobj = ($.grep(songlist, function(e){return e.artist == cursong[0].artist})); console.log(songlist); imgcode = "<a href='" + songobj[0].link + "'><img id = 'spotifylogo' src='images/spotify.jpg' alt='miage'></a>"; thevalue = imgcode + "<br>" + "available on spotify " + songobj[0].price + "<br> <br>"; $("#songdisplay").append(thevalue); deferreds = [], index; }); } function runjson(network,searchtype,display){ var url = returnurl(network,searchtype,$('#song_field').val().split(' ').join("+")); var val = $('#song_field').val(); var info = {val: val} deferreds.push($.getjson(url, data, function(data){ if(document.getelementbyid("box") !== "undefined" && display == true){ clearscreen(); createcols(); } songlist = []; eval(network + "(data);"); if(display === true){ for(i = 0; < songlist.length; i++) { displaytrack(i); } } done = true; })); }

your code says this:

start bunch of asynchronous operations wait them finish ($.when.apply(...)) process songlist array

http://api.jquery.com/jquery.when/

in case multiple deferred objects passed jquery.when(), method returns promise new "master" deferred object tracks aggregate state of deferreds has been passed. method resolve master deferred all deferreds resolve

each 1 of asynchronous operations goes

clear out songlist array add info songlist array

so hence flow going go (for example; order may vary)

clear out songlist, add together list 1 songlist clear out songlist, add together list 2 songlist ... clear out songlist, add together list n songlist process songlist

i.e. whichever asynchronous operation happens execute lastly clears out whatever in there before , adds list.

if want stick using global songlist variable, instead of clearing out in each runjson invocation, clear out before loop , append results long list. or maintain them separate, have array of arrays (for example).

jquery getjson

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -