Use the When in jquery to implement multiple AJAX requests corresponding to a single callback example sharing _jquery

Source: Internet
Author: User
Tags getscript dojo toolkit

I know that these functions are executed asynchronously (asyncronously) and will delay the return for some time, so I wonder if there is a way that I can use a single callback to load them in parallel, just as the JS loader Curljs does. Very lucky! With Jquery.when, I can load two requests concurrently and perform only one callback!

JQuery Script
As I mentioned, here are the use cases for loading scripts and a JSON resource:

Copy Code code as follows:

$.when (
$.getscript ('/media/js/wiki-min.js?build=21eb633 '),
$.getjson (' https://developer.mozilla.org/en-US/demos/feeds/json/featured/')
). Then (function (A, B) {//or you can also use ". Done"
Yay, loading complete, here we can perform some dependent operation ...
});

When the resource load completes, the specified done or then callback is triggered, so you can know that the request has been completed. Each request returns a different type of callback parameter object, so the request may return the following information:

Copy Code code as follows:

Format: [Response, State, JQXHR], [Response, State, JQXHR]
["(function (c) {var e=c (". From-search-navigate "); if (e ...;;; if (j) {g.apply (m,l)}}}) (Window,document,jquery); ", success, Object]
[array[15], "success", Object]

If you need to add a traditional Ajax XHR request, such as a widget template, we can do this:

Copy Code code as follows:

$.when (
$.getscript ('/media/js/wiki-min.js?build=21eb633 '),
$.getjson (' https://developer.mozilla.org/en-US/demos/feeds/json/featured/'),
$.get ('/')
). Then (function (A, B, c) {
Console.log (A, B, c);
});

Dojo Toolkit has this kind of functionality early on, but jquery can do the same. I'm pretty excited. For today's development, multiple requests that are not synchronized and are not yet determined to return sequentially share the same callback is a natural requirement, so jquery is definitely advancing with the times!

Related Article

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.