Wednesday 15 June 2011

jquery - Javascript callbacks gets processed faster than others -



jquery - Javascript callbacks gets processed faster than others -

i'm using javascript sdk plugin facebook create feed on webpage.

the problem during load feed gets unordered, if have setup callback chain.

i think gets unordered because "second" async phone call gets processed faster "first" async call.

this first time i've been using callbacks, doing right?

how can solve feed gets unordered if calls finish faster others?

the code below relevant code , under working status.

function initfeed(){ fb.api('/{id}/feed', function(response){ var feedarray = response.data; $.each(feedarray, function(){ var $this = $(this)[0]; //status object single status in feed setstatus($this, processstatus); //processstatus function defined below }); }); } function setstatus(statusobject, callbackprocessstatus){ fb.api("/{personid}?fields=id,link,name,picture", function (response) { var html = /* generates html based statusobject , response */ callbackprocessstatus(html); }); } function processstatus(html){ $('#fb-status-wrapper').append(html); }

(was uncertain on title of post, please edit if think not descriptive enough) best regards

indeed cannot rely on order in requests finish. way sure, phone call sec 1 if first 1 done. slow downwards loading quite lot.

another possibility remember each request 1 is, , insert items in right order (insert before 'later' one, if 1 received earlier).

i think easiest way that, create placeholders items within each loop, placeholders inserted in right order. when requests return, place responses in right placeholder.

it this. 2 lines , couple of tiny changes. couldn't test without api, hope idea.

function initfeed(){ fb.api('/{id}/feed', function(response){ var feedarray = response.data; $.each(feedarray, function(index){ var $this = $(this)[0]; //status object single status in feed // create container per item within wrapper. var $itemcontainer = $('<div></div>'); $('#fb-status-wrapper').append($itemcontainer); // pass container api function. setstatus($this, processstatus, $itemcontainer); //processstatus function defined below }); }); } function setstatus(statusobject, callbackprocessstatus, $container){ fb.api("/{personid}?fields=id,link,name,picture", function (response) { var html = /* generates html based statusobject , response */ // pass item place holder/container processing procedure. callbackprocessstatus(html, $container); }); } function processstatus(html, $container){ $container.append(html); }

javascript jquery facebook-javascript-sdk

No comments:

Post a Comment