JQuery defered对象的个人理解

关于Defered对象,主要包含以下一些常用的方法:

methodsversionargsdescription
deferred.always(alwaysCallbacks [, alwaysCallbacks])1.6A function, or array of functions, that is called when the Deferred is resolved or rejected.Add handlers to be called when the Deferred object is either resolved or rejected.
deferred.done(doneCallbacks [, doneCallbacks ])1.5A function, or array of functions, that are called when the Deferred is resolved.Add handlers to be called when the Deferred object is resolved.
deferred.fail( failCallbacks [, failCallbacks ])1.5A function, or array of functions, that are called when the Deferred is rejected.Add handlers to be called when the Deferred object is rejected.
deferred.then(doneFilter [, failFilter ] [, progressFilter ] )1.8doneFilter: A function that is called when the Deferred is resolved.
failFilter: An optional function that is called when the Deferred is rejected.
progressFilter: An optional function that is called when progress notifications are sent to the Deferred.
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
deferred.catch(failFilter )3.0A function that is called when the Deferred is rejected.Add handlers to be called when the Deferred object is rejected.
deferred.progress()1.7A function, or array of functions, to be called when the Deferred generates progress notifications.Add handlers to be called when the Deferred object generates progress notifications.
deferred.notify(args)1.7Optional arguments that are passed to the progressCallbacks.Call the progressCallbacks on a Deferred object with the given args.
deferred.notifyWith( context [, args ] )1.7context: Context passed to the progressCallbacks as the this object.
args: An optional array of arguments that are passed to the progressCallbacks.
Call the progressCallbacks on a Deferred object with the given context and args.
deferred.reject([args ] )1.5Optional arguments that are passed to the failCallbacks.Reject a Deferred object and call any failCallbacks with the given args.
deferred.rejectWith( context [, args ])1.5context: Context passed to the failCallbacks as the this object.
args: An optional array of arguments that are passed to the failCallbacks.
Reject a Deferred object and call any failCallbacks with the given context and args.
deferred.resolve([args ] )1.5Optional arguments that are passed to the doneCallbacks.Resolve a Deferred object and call any doneCallbacks with the given args.
deferred.resolveWith(context [, args ] )1.5context: Context passed to the doneCallbacks as the this object.
args: An optional array of arguments that are passed to the doneCallbacks.
Resolve a Deferred object and call any doneCallbacks with the given context and args.
deferred.state()1.7This method does not accept any arguments.Determine the current state of a Deferred object. Returns: “pending” or “resolved” or “rejected”
”pending”: The Deferred object is not yet in a completed state (neither “rejected” nor “resolved”).
deferred.promise([target ] )1.5Object onto which the promise methods have to be attachedReturn a Deferred’s Promise object.

其他详细信息请参照JQuery Defered 官网

如下是一个简单的例子:

$(function () {
    var def = $.Deferred();
    def.always(function (...val) {
            console.log(this);
            console.log(val);
            console.log("always: " + val + " status: " + def.state());
        })
        .done(function (val) {
            console.log("done: " + val + " status: " + def.state());
        })
        .then(function (val) {
            console.log("then: " + val + " status: " + def.state());
        })
        .pipe(function (pipe) {
            console.log("pipe: " + pipe + " status: " + def.state());
        })
        .fail(function (err) {
            console.log("fail: " + err + " status: " + def.state());
        })
        .catch(function (e) {
            console.log("catch: " + e + " status: " + def.state());
        })
        .progress(function (p) {
            console.log("progress> " + p);
        });
    def.notify('notify').notify('notify1').resolveWith({a:1, b:2}, [3,4,5,6,7]);
    // def.notify('notify').notify('notify1').reject('reject');
});
//{a: 1, b: 2}
//(5) [3, 4, 5, 6, 7]
//always: 3,4,5,6,7 status: resolved
//done: 3 status: resolved
//pipe: 3 status: resolved
//then: undefined status: resolved
//progress> notify
//progress> notify1

注意:

  • resolveWith的第一个参数是always | done 函数的 this,第二个参数是一个数组,always | done 函数需要使用(…val)方式去接收。
  • notify可以调用多次,都会被progress所接收
  • done函数执行后,若是后面还有其他的done | then 函数时,参数不为空,但若是中间跟有其他fail | pipe | progress | catch 函数时,参数为undefined。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值