简体   繁体   中英

Mootools XHR request: synchronous and/or chained?

I have a couple of XHR requests, which are handled by the Mootools Request class. This class offers some options to time the requests appropriately. What I'm doing:

  1. XHR: Post form data
  2. XHR: Refresh main pane
  3. XHR: Refresh subpane

Of course, requests 2 & 3 must wait before 1 is finished. So these are triggered within the onComplete event handler. However, the Request class offers options for handling multiple XHR requests. My question is about these two:

  1. The option link can be set to chain , in order to 'chain' them, or, as the Moo docs state:

    Any calls made to start while the request is running will be chained up, and will take place as soon as the current request has finished, one after another.

  2. The option async can be set to false , to prevent later requests from executing. According to the Moo docs:

    If set to false, the requests will be synchronous and freeze the browser during request.

Apart from the browser freezing part, what is exactly the difference? Which one should I use for request no. 1? Is it better to do it synchronously, so I'm sure nothing else executes in the meantime? And how about using both, does that make any sense?

well. the difference between link: chain and async: false is simple.

first axiom - you are reusing your request instance and not making a new one. even if you are not, it can work with async. eg, if you have async: false , then this code:

new Request({async:false}).send();

// this one below will not run until the UI thread has finished
new Request({async:false}).send();

// nor will this
somefunc();

if you go with chain:

var req = new Request({link: "chain"});

req.send();
// this won't run until the previous request has completed:
req.send();
// this will run independently of the above and may finish first as
// they are not synchronous and this is a brand new instance.
new Request().send();

The chained requests are asynchronous, when one ends it triggers the second one and so on, so you can have several requests without jamming the browser with all the requests at the same time.

The chained requests do not freeze your browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM