Analog Ajax's same asynchronous

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.