惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 深蓝--广州

AngularJs angular.identity和angular.noop详解 css中clearfix清除浮动的用法及其原理示例介绍 opacity与RGBA透明的区别 CSS3 Gradient javascript语言精粹 严格模式认识 使用javascript打开一个新页而不被浏览器屏蔽 关于浏览器缓存 ie6下js更新元素display:block后,仍然不显示的hack办法 jQuery jsonp无法捕获404、500状态错误 移动端开发问题整理 修改input的type属性 transform:rotate在手机上显示有锯齿的解决方案 javascript钩子机制 jQuery.validate 常用方法及注意问题 SWFObject推出2使用示例 .NET种Json时对单引号和特殊字符串的处理 自定义枚举类型注释属性,并在程序中获取 Python Win32 Extensions
Promise与Defer认识
深蓝--广州 · 2016-05-19 · via 博客园 - 深蓝--广州

1、deffer对象:jquery的回掉函数解决方案;含义是延迟到未来某个点再执行;

2、$.ajax链式写法:

$.ajax("test.php")

    .done(function() { console.log("success"); })   

    .fail(function() { console.log("fail"); })

3、指定同一操作的多个回调函数:

$.ajax("test.php")

    .done(function() { console.log("success"); })   

    .fail(function() { console.log("fail"); })

    .done(function() { console.log("success two"); })

4、为多个操作指定回调函数:若其中一个执行失败则执行fail回调;

$.when($.ajax("test.php"), $.ajax("test2.php"))

   .done(function() { console.log("success"); })   

   .fail(function() { console.log("fail"); })

5、普通操作的回调函数接口:

var dtd = $.Deferred(); 

var wait = function (dtd) {

    var tasks = function() {

        alert("执行完毕!");

        dtd.resole(); 

    }

    setTimeout(tasks, ,5000);

    return dtd;

}

$.when(wait(dtd))

    .done(function() { console.log("success"); })   

    .fail(function() { console.log("fail"); })

6、用promise优化 全局defer对象,防止执行状态被外部改变:

var wait = function () {

    var dtd = $.Deferred(); 

    var tasks = function() {

        alert("执行完毕!");

        dtd.resole(); 

    }

    setTimeout(tasks, ,5000);

    return dtd.promise(); 

};

$.when(wait())

    .done(function() { console.log("success"); })   

    .fail(function() { console.log("fail"); })

7、另一种防止执行状态被外部改变的方法:用deferred的构建函数:

$.Deferred(wait) 

   .done(function() { console.log("success"); })   

   .fail(function() { console.log("fail"); })

8、最后一种防止执行状态被外部改变的方法:在wait对象上部署deferred接口:

var dtd = $.Defferred();

var wait = function(dtd) {

    var tasks = function() {

        alert("执行完毕!");

        dtd.resole(); 

    }

    setTimeout(tasks, ,5000);

}

dtd.promise(wait);

wait.done(function() { console.log("success"); })        

    .fail(function() { console.log("fail"); })

wait(dtd);

小结:deferred对象的方法:

(1) $.Deferred() 生成一个deferred对象。

(2) deferred.done() 指定操作成功时的回调函数

(3) deferred.fail() 指定操作失败时的回调函数

(4) deferred.promise() 没有参数时,返回一个新的deferred对象,该对象的运行状态无法被改变;接受参数时,作用为在参数对象上部署deferred接口。

(5) deferred.resolve() 手动改变deferred对象的运行状态为"已完成",从而立即触发done()方法。

(6)deferred.reject() 这个方法与deferred.resolve()正好相反,调用后将deferred对象的运行状态变为"已失败",从而立即触发fail()方法。

(7) $.when() 为多个操作指定回调函数。

(8)deferred.then()有时为了省事,可以把done()和fail()合在一起写,这就是then()方法。

$.when($.ajax( "/main.php" )).then(successFunc, failureFunc );

如果then()有两个参数,那么第一个参数是done()方法的回调函数,第二个参数是fail()方法的回调方法。

如果then()只有一个参数,那么等同于done()。

(9)deferred.always()这个方法也是用来指定回调函数的,它的作用是,不管调用的是deferred.resolve()还是deferred.reject(),最后总是执行。

$.ajax( "test.html" ).always( function() { alert("已执行!");} );