It suddenly occurred to me today that only in the app, if the data is requested by the app-provided interface, if the interface is not asynchronous, what to do.
So it was churn down. Simulates Ajax with Asynchrony.
1 varVshopdataapi = {2Queryarr: [],//{code: [{Code, data, callback, is_sync}]}//system queue3Is_sync:false,//whether to synchronize default false for unsynchronized system variables4Ajax:function(item, callback) {//Custom5 //var time = parseint (Math.random () * +);6 //Console.log (' +++++ sent successfully + ' +item.code+ ' +++++ ');7 //setTimeout (function () {8 ////Analog Timer callback9 //callback (' Order: ' +item.code+ ' + (Item.is_sync? ' Synchronous ': ' async '));Ten //}, time * +); One } A } - - the /** - * in Queue - * @param {Object} item must contain field callback: Callback function, Is_sync: whether synchronizing - */ +Vshopdataapi.push =function(item) { -Item.is_sync = Item.is_sync | |false; + A if(vshopdataapi.is_sync) { at VshopDataApi.queryArr.push (item); - return; - } - - if(item.is_sync) { -Vshopdataapi.is_sync =Item.is_sync; in } - to //Send + Vshopdataapi._ajax (item); - the }; * $ //out QueuePanax NotoginsengVshopdataapi.shift =function(){ - if(VshopDataApi.queryArr.length <= 0) {return;} the varitem = {}; +item =VshopDataApi.queryArr.shift (); AVshopdataapi.is_sync =Item.is_sync; the Vshopdataapi._ajax (item); + if(!Vshopdataapi.is_sync) { - Vshopdataapi.shift (); $ } $ - }; - the //System Execution -Vshopdataapi._ajax =function(item) {Wuyi the(function(item) { - WuVshopdataapi.ajax (Item,function(Result) { - item.callback (result); About $ if(item.is_sync) { - Vshopdataapi.shift (); - } - }); A }) (item); + }; the - //callback function $ functioncallback (Result) { theConsole.log ('---------callback begin----------'); the Console.log (result); theConsole.log ('--------callback end---------'); the }; - in //rewrite theVshopdataapi.ajax =function(item, callback) {//Custom the varTime = parseint (Math.random () * 100); AboutConsole.log (' +++++ send success + ' +item.code+ ' +++++ '); theSetTimeout (function (){ the //Analog unscheduled callbacks theCallback (' Order: ' +item.code+ ' + (Item.is_sync? ' Sync ': ' Async ')); +}, Time * 100); - } the Bayi //into the queue theVshopdataapi.push ({code: ' 1 ', data: {}, Callback:callback, Is_sync:false }); theVshopdataapi.push ({code: ' 2 ', data: {}, Callback:callback, Is_sync:true});//Sync -Vshopdataapi.push ({code: ' 3 ', data: {}, Callback:callback, Is_sync:false }); -Vshopdataapi.push ({code: ' 4 ', data: {}, Callback:callback, Is_sync:false }); theVshopdataapi.push ({code: ' 5 ', data: {}, Callback:callback, Is_sync:true});//Sync theVshopdataapi.push ({code: ' 6 ', data: {}, Callback:callback, Is_sync:false }); theVshopdataapi.push ({code: ' 7 ', data: {}, Callback:callback, Is_sync:true});//Sync theVshopdataapi.push ({code: ' 8 ', data: {}, Callback:callback, Is_sync:false});
Write a random time at the callback. See if the synchronization succeeds first, will not continue execution.
Results:
Analog Ajax's same asynchronous