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!