Jquery异步 Deferred Object
Deferred Object
function f1() {
var dfd = $.Deferred();
setTimeout(function () {
dfd.resolve();// resolved, rejected, or still in progress.
}, 500);
return dfd.promise();
}
f1().done(function () {
console.log("Hello World !")
}).fail(function () {
console.log("Error")
});
var wait = function (dtd) {
setTimeout(function () {
dtd.resolve();//表示处理结束, 调用后立即执行.done方法
//dtd.reject();//标识处理失败,调用后立即执行fail方法
}, 500);
return dtd.promise();
};
//使用$.when()为普通操作添加回调函数 为多个操作指定回调函数
//$.when(deferred, deferred,...):多有的延迟对象都调用了resolve()时才执行done;只要有一个延迟对象调用了reject()就会执行fail()。
//如果参数不是延迟对象就会跳过该参数并代表成功,如果参数都不是延迟对象,也会成功,会执行done。
//成功或失败的回调函数的参数对应when中的参数,如果参数是延迟对象回调函数得到undefined,不是延迟对象得到参数值。
//done中的方法可以接受参数由when调用的方法返回
var dtd = $.Deferred();
$.when(wait(dtd), wait(dtd))
.done(function (p1, p2) {
console.log("when 多个方法执行成功 ")
})
.fail(function () {
console.log("Error")
});
//还可以直接在wait对象上部署deferred接口。
$.Deferred(wait)
.done(function () {
console.log("Success ")
})
.fail(function () {
console.log("Error")
});
// 为了省事,可以把done()和fail()合在一起写,这就是then()方法。
$.Deferred(wait)
.then(function () {
console.log("Success ")
}, function () {
console.log("Error")
}).always(function () {
console.log("总是执行的方法");
});
/* 如果then()有两个参数,那么
第一个参数是done()方法的回调函数,
第二个参数是fail()方法的回调方法。
如果then()
只有一个参数,那么等同于done()。*/
deferred.always()
Add handlers to be called when the Deferred object is either resolved or rejected.
deferred.done()
Add handlers to be called when the Deferred object is resolved.
deferred.fail()
Add handlers to be called when the Deferred object is rejected.
deferred.isRejected()
Determine whether a Deferred object has been rejected.
deferred.isResolved()
Determine whether a Deferred object has been resolved.
deferred.notify()
Call the progressCallbacks on a Deferred object with the given args.
deferred.notifyWith()
Call the progressCallbacks on a Deferred object with the given context and args.
Also in: Deprecated > Deprecated 1.8
deferred.pipe()
Utility method to filter and/or chain Deferreds.
deferred.progress()
Add handlers to be called when the Deferred object generates progress notifications.
deferred.promise()
Return a Deferred’s Promise object.
deferred.reject()
Reject a Deferred object and call any failCallbacks with the given args.
deferred.rejectWith()
Reject a Deferred object and call any failCallbacks with the given context and args.
deferred.resolve()
Resolve a Deferred object and call any doneCallbacks with the given args.
deferred.resolveWith()
Resolve a Deferred object and call any doneCallbacks with the given context and args.
deferred.state()
Determine the current state of a Deferred object.
deferred.then()
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
jQuery.Deferred()
A factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.
Also in: Core
jQuery.when()
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
.promise()
Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished.
Jquery异步 Deferred Object的更多相关文章
- jQuery异步Deferred
原文链接:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 普通 ...
- jQuery源码学习:Deferred Object
本文所有讨论均基于jQuery版本3.1.1,官网http://jquery.com/. 一.Deferred Object 1. 简介和创建 详见API:http://api.jquery.com/ ...
- jQuery异步框架探究2:jQuery.Deferred方法
(本文针对jQuery1.6.1版本号)关于Deferred函数的描写叙述中有一个词是fledged,意为"羽翼丰满的",说明jQuery.Deferred函数应用应该更成熟. 这 ...
- jQuery之Deferred源码剖析
一.前言 大约在夏季,我们谈过ES6的Promise(详见here),其实在ES6前jQuery早就有了Promise,也就是我们所知道的Deferred对象,宗旨当然也和ES6的Promise一样, ...
- jquery 之 Deferred 使用与实现
观察者模式是开发中经常使用的模式,这个模式由两个主要部分组成:主题和观察者.通过观察者模式,实现主题和观察者的解耦. 主题负责发布内容,而观察者则接收主题发布的内容.通常情况下,观察者都是多个,所以, ...
- jQuery的deferred对象
应用场景:处理异步任务 看到一篇阮一峰老师的博客挺好的讲的就是jQuery的deferred对象.坦诚讲之前没有怎么用过这个东东呢. 摘其中几点记录下 (1) $.Deferred() 生成一个def ...
- ASP.NET MVC验证 - jQuery异步验证
本文主要体验通过jQuery异步验证. 在很多的教材和案例中,MVC验证都是通过提交表单进行的.通过提交表单,可以很容易获得验证出错信息.因为,无论是客户端验证还是服务端验证,总能找到与Model属性 ...
- MVC验证11-对复杂类型使用jQuery异步验证
原文:MVC验证11-对复杂类型使用jQuery异步验证 本篇体验使用"jQuery结合Html.BeginForm()"对复杂类型属性进行异步验证.与本篇相关的"兄弟篇 ...
- jquery的deferred使用详解
原文:hhtps://www.cnblogs.com/shijingjing07/p/6403450.html -------------------------------------------- ...
随机推荐
- 假如想要建设一个能承受500万PV/每天的网站,服务器每秒要处理多少个请求才能应对?
假如想要建设一个能承受500万PV/每天的网站,服务器每秒要处理多少个请求才能应对?如何计算? 1.PV是什么:PV是page view的简写.PV是指页面的访问次数,每打开或刷新一次页面,就算做一个 ...
- Linux-(kill,wc,killall,ln,cal,date)
kill命令 1.命令格式: kill [参数] [进程号] 2.命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果仍无法终止该程序可用“-KILL” 参数 ...
- WPF在XAML的资源中定义空字符串String.Empty
代码如下: <!--1. 首先引用System的命名空间--> <Window x:Class="DriverEasyWPF.Views.DialogWindow" ...
- spark集群搭建(java)未完待续
环境 操作系统:windows10 虚拟机工具:VMware14.1 NUX版本:Centos7.2(64) JDK:1.8(64) 一.安装linux,master(桥接模式上网),slave(na ...
- [转]9.2.3 .net core 通过TagHelper封装控件
本文转自:https://www.cnblogs.com/BenDan2002/p/6170624.html .net core 除了继续保留.net framework的HtmlHelper的写法以 ...
- [转]微信小程序之购物车 —— 微信小程序实战商城系列(5)
本文转自:http://blog.csdn.net/michael_ouyang/article/details/70755892 续上一篇的文章:微信小程序之商品属性分类 —— 微信小程序实战商城 ...
- s11d27 算法
s11d27 算法 一.理论 1.1 时间复杂度和空间复杂度的理论: 1)空间复杂度: 是程序运行所以需要的额外消耗存储空间,一般的递归算法就要有o(n)的空间复杂度了, 简单说就是递归集算时通常是反 ...
- 设计模式学习--面向对象的5条设计原则之Liskov替换原则--LSP
一.LSP简介(LSP--Liskov Substitution Principle): 定义:如果对于类型S的每一个对象o1,都有一个类型T的对象o2,使对于任意用类型T定义的程序P,将o2替换为o ...
- WPF 从文件加载字体
本文告诉大家从文件加载字体.在wpf 使用 fontfamily 显示指定的 ttf 显示字体 假如有字体在 C:\Projects\MyProj\free3of9.ttf ,可以使用 Private ...
- MySQL3:存储过程和函数
什么是存储过程 简单说,存储过程就是一条或多条SQL语句的集合,可视为批文件,但是起作用不仅限于批处理.本文主要讲解如何创建存储过程和存储函数以及变量的使用,如何调用.查看.修改.删除存储过程和存储函 ...
function f1() {
var dfd = $.Deferred();
setTimeout(function () {
dfd.resolve();// resolved, rejected, or still in progress.
}, 500);
return dfd.promise();
}
f1().done(function () {
console.log("Hello World !")
}).fail(function () {
console.log("Error")
});
var wait = function (dtd) {
setTimeout(function () {
dtd.resolve();//表示处理结束, 调用后立即执行.done方法
//dtd.reject();//标识处理失败,调用后立即执行fail方法
}, 500);
return dtd.promise();
};
//使用$.when()为普通操作添加回调函数 为多个操作指定回调函数
//$.when(deferred, deferred,...):多有的延迟对象都调用了resolve()时才执行done;只要有一个延迟对象调用了reject()就会执行fail()。
//如果参数不是延迟对象就会跳过该参数并代表成功,如果参数都不是延迟对象,也会成功,会执行done。
//成功或失败的回调函数的参数对应when中的参数,如果参数是延迟对象回调函数得到undefined,不是延迟对象得到参数值。
//done中的方法可以接受参数由when调用的方法返回
var dtd = $.Deferred();
$.when(wait(dtd), wait(dtd))
.done(function (p1, p2) {
console.log("when 多个方法执行成功 ")
})
.fail(function () {
console.log("Error")
});
//还可以直接在wait对象上部署deferred接口。
$.Deferred(wait)
.done(function () {
console.log("Success ")
})
.fail(function () {
console.log("Error")
});
// 为了省事,可以把done()和fail()合在一起写,这就是then()方法。
$.Deferred(wait)
.then(function () {
console.log("Success ")
}, function () {
console.log("Error")
}).always(function () {
console.log("总是执行的方法");
});
/* 如果then()有两个参数,那么
第一个参数是done()方法的回调函数,
第二个参数是fail()方法的回调方法。
如果then()
只有一个参数,那么等同于done()。*/
deferred.always()
Add handlers to be called when the Deferred object is either resolved or rejected.
deferred.done()
Add handlers to be called when the Deferred object is resolved.
deferred.fail()
Add handlers to be called when the Deferred object is rejected.
deferred.isRejected()
Determine whether a Deferred object has been rejected.
deferred.isResolved()
Determine whether a Deferred object has been resolved.
deferred.notify()
Call the progressCallbacks on a Deferred object with the given args.
deferred.notifyWith()
Call the progressCallbacks on a Deferred object with the given context and args.
deferred.pipe()
Utility method to filter and/or chain Deferreds.
deferred.progress()
Add handlers to be called when the Deferred object generates progress notifications.
deferred.promise()
Return a Deferred’s Promise object.
deferred.reject()
Reject a Deferred object and call any failCallbacks with the given args.
deferred.rejectWith()
Reject a Deferred object and call any failCallbacks with the given context and args.
deferred.resolve()
Resolve a Deferred object and call any doneCallbacks with the given args.
deferred.resolveWith()
Resolve a Deferred object and call any doneCallbacks with the given context and args.
deferred.state()
Determine the current state of a Deferred object.
deferred.then()
Add handlers to be called when the Deferred object is resolved, rejected, or still in progress.
jQuery.Deferred()
A factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.
jQuery.when()
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
.promise()
Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished.
原文链接:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 普通 ...
本文所有讨论均基于jQuery版本3.1.1,官网http://jquery.com/. 一.Deferred Object 1. 简介和创建 详见API:http://api.jquery.com/ ...
(本文针对jQuery1.6.1版本号)关于Deferred函数的描写叙述中有一个词是fledged,意为"羽翼丰满的",说明jQuery.Deferred函数应用应该更成熟. 这 ...
一.前言 大约在夏季,我们谈过ES6的Promise(详见here),其实在ES6前jQuery早就有了Promise,也就是我们所知道的Deferred对象,宗旨当然也和ES6的Promise一样, ...
观察者模式是开发中经常使用的模式,这个模式由两个主要部分组成:主题和观察者.通过观察者模式,实现主题和观察者的解耦. 主题负责发布内容,而观察者则接收主题发布的内容.通常情况下,观察者都是多个,所以, ...
应用场景:处理异步任务 看到一篇阮一峰老师的博客挺好的讲的就是jQuery的deferred对象.坦诚讲之前没有怎么用过这个东东呢. 摘其中几点记录下 (1) $.Deferred() 生成一个def ...
本文主要体验通过jQuery异步验证. 在很多的教材和案例中,MVC验证都是通过提交表单进行的.通过提交表单,可以很容易获得验证出错信息.因为,无论是客户端验证还是服务端验证,总能找到与Model属性 ...
原文:MVC验证11-对复杂类型使用jQuery异步验证 本篇体验使用"jQuery结合Html.BeginForm()"对复杂类型属性进行异步验证.与本篇相关的"兄弟篇 ...
原文:hhtps://www.cnblogs.com/shijingjing07/p/6403450.html -------------------------------------------- ...
假如想要建设一个能承受500万PV/每天的网站,服务器每秒要处理多少个请求才能应对?如何计算? 1.PV是什么:PV是page view的简写.PV是指页面的访问次数,每打开或刷新一次页面,就算做一个 ...
kill命令 1.命令格式: kill [参数] [进程号] 2.命令功能: 发送指定的信号到相应进程.不指定型号将发送SIGTERM(15)终止指定进程.如果仍无法终止该程序可用“-KILL” 参数 ...
代码如下: <!--1. 首先引用System的命名空间--> <Window x:Class="DriverEasyWPF.Views.DialogWindow" ...
环境 操作系统:windows10 虚拟机工具:VMware14.1 NUX版本:Centos7.2(64) JDK:1.8(64) 一.安装linux,master(桥接模式上网),slave(na ...
本文转自:https://www.cnblogs.com/BenDan2002/p/6170624.html .net core 除了继续保留.net framework的HtmlHelper的写法以 ...
本文转自:http://blog.csdn.net/michael_ouyang/article/details/70755892 续上一篇的文章:微信小程序之商品属性分类 —— 微信小程序实战商城 ...
s11d27 算法 一.理论 1.1 时间复杂度和空间复杂度的理论: 1)空间复杂度: 是程序运行所以需要的额外消耗存储空间,一般的递归算法就要有o(n)的空间复杂度了, 简单说就是递归集算时通常是反 ...
一.LSP简介(LSP--Liskov Substitution Principle): 定义:如果对于类型S的每一个对象o1,都有一个类型T的对象o2,使对于任意用类型T定义的程序P,将o2替换为o ...
本文告诉大家从文件加载字体.在wpf 使用 fontfamily 显示指定的 ttf 显示字体 假如有字体在 C:\Projects\MyProj\free3of9.ttf ,可以使用 Private ...
什么是存储过程 简单说,存储过程就是一条或多条SQL语句的集合,可视为批文件,但是起作用不仅限于批处理.本文主要讲解如何创建存储过程和存储函数以及变量的使用,如何调用.查看.修改.删除存储过程和存储函 ...