【发布时间】:2015-08-25 03:40:01
【问题描述】:
我找不到jQuery 承诺的delay 或wait 函数。我在 SO (Using jQuery.Deferred to avoid nested setTimeout callbacks) 上找到了一个函数:
function delay(time) {
return function () {
console.log("Delaying");
var ret = new $.Deferred();
setTimeout(function () {
ret.resolve();
}, time);
return ret;
};
}
而且,这就是我使用它的方式:
run: function () {
return $()
.promise()
.then(function () {
console.log("call together");
console.log("call together");
})
.then(delay(2000))
.then(function () {
console.log("call first");
})
.then(delay(2000))
.then(function () {
console.log("call second");
})
}
我想扩展我可以写的承诺或延迟对象:
run: function () {
return $()
.promise()
.then(function () {
console.log("call together");
console.log("call together");
})
.delay(2000)
.then(function () {
console.log("call first");
})
.delay(2000)
.then(function () {
console.log("call second");
})
}
【问题讨论】:
-
@guest271314:我需要完全符合承诺的功能。这个函数用于效果。
-
Is it possible to add methods to JQuery's promise object? 的可能重复项(只需添加
.delay方法而不是.catch) -
@jfriend00:当然是it is possible……但不像扩展原型那么容易。
-
@jfriend00:该代码适用于在装饰器代码运行后构建的所有延迟和承诺(包括 jqXHR)。但是,如果您还有其他问题,让我们在那里讨论:-)
标签: jquery promise jquery-deferred