这里有一个测试代码:
var dfd_1 = $.Deferred(),
dfd_2 = $.Deferred(),
dfd_3 = $.Deferred();
function test(content, waitTime, dfd) {
console.log(content + ' begin');
setTimeout(function() {
console.log(content + ' end');
dfd.resolve();
}, waitTime);
return dfd.promise();
}
$.when(test('first', 2000, dfd_1), test('second', 4000, dfd_2), test('third', 6000, dfd_3))
.then(function() {
console.log('all done');
});控制台的结果是:
first begin
second begin
third begin
first end
second end
third end
all done3函数一步一步地开始和结束,但我需要这样的结果(开始结束,开始结束.):
first begin
first end
second begin
second end
third begin
third end
all done如何使$.Deferred逐步工作?请帮我解决问题
发布于 2015-09-23 13:42:08
使用then()
test('first', 2000, dfd_1)
.then(function() {
return test('second', 4000, dfd_2);
})
.then(function() {
return test('third', 6000, dfd_3);
})
.then(function() {
console.log('all done');
})
;小提琴:http://jsfiddle.net/saj5stt9/
https://stackoverflow.com/questions/32740871
复制相似问题