Home > Web Front-end > JS Tutorial > How to handle batch deferreds in jquery_jquery

How to handle batch deferreds in jquery_jquery

WBOY
Release: 2016-05-16 17:03:09
Original
981 people have browsed it

This code is modeled after the implementation of $.when() in jquery source code

Copy code The code is as follows:

function test(i) {
var dfd = $.Deferred();
if(i%2 == 0) {
console.log("resolve " i);
dfd.resolve();
} else {
console.log("failure " i);
dfd.reject();
}

return dfd.promise();
}
function call() {
var dfd = $.Deferred();
var remain = 10;
for(var i=0;i< 10;i ){
test(i).done(function() {
                                                                                                          (--remain)) {
          dfd.resolve();
call().done(function() {
console.log("all finished");
});



Output result:



Copy code

The code is as follows:

resolve 0 test.js:4

failure 1 test.js:7resolve 2 test.js:4failure 3 test.js:7resolve 4 test.js:4failure 5 test.js:7resolve 6 test.js:4failure 7 test.js:7
resolve 8 test.js:4
failure 9 test.js:7
all finished


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template