怎样链递延功能的jQuery 1.8.x的顺序? [英] How do I chain a sequence of deferred functions in jQuery 1.8.x?
本文介绍了怎样链递延功能的jQuery 1.8.x的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于这些功能:
function func1() {
var dfd = $.Deferred();
setTimeout(function() {
dfd.resolve('Password');
}, 1000);
return dfd.promise();
}
function func2(message) {
var dfd = $.Deferred();
setTimeout(function() {
if (message == 'Password') {
dfd.resolve('Hello World');
}
}, 1000);
return dfd.promise();
}
我想找到一个更好的办法来做到以下几点。注:这是使用jQuery 1.8.x.
I'd like to find a better way to do the following. Note this is using jQuery 1.8.x.
var promise = func1();
promise.done(function(message1) {
var promise2 = func2(message1);
promise2.done(function(message2) {
alert(message2);
});
});
任何想法?我想使用jQuery或#PIPE将#then工作,但我无法弄清楚。这里是一个小提琴打转转: http://jsfiddle.net/Z7prn/
推荐答案
这不是那么复杂(或者使用。然后
或 .pipe
,他们都是因为jQuery的1.8,我认为是相同的)。
It's not that complicated (either use .then
or .pipe
, they are both the same since jQuery 1.8 I think).
promise.then(func2).done(function(message) {
alert(message);
});
由于 FUNC2
返回一个新的延期对象时,<code> .done 回调连接到一个替代。
Since func2
returns a new deferred object, the .done
callback is attached to that one instead.
这篇关于怎样链递延功能的jQuery 1.8.x的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文