您的位置:首页 > Web前端 > JQuery

Jquery promise

2014-04-22 18:13 295 查看
Jquery Promis

Success =========>>> done

Fail =========>>> fail

Features:

1.support the "Chain" Call Back

$.ajax("test.html")

.done(function(){ alert("S1!");} )

.fail(function(){ alert("F1"); } )

.done(function(){ alert("S2");} );

-------If success, alert("S1") then alert("S2")

1.support mutiple call point to one Call Back

$.when($.ajax("test1.html"), $.ajax("test2.html"))

.done(function(){ alert("哈哈,成功了!"); })

.fail(function(){ alert("出错啦!"); });
[b]-------Once two ajax call both are success ,then call done(),either call fail()[/b]

3.Three status for the deferred object

要说清楚这个问题,就要引入一个新概念"执行状态"。jQuery规定,deferred对象有三种执行状态----未完成,已完成和已失败。如果执行状态是"已完成"(resolved),deferred对象立刻调用done()方法指定的回调函数;如果执行状态是"已失败",调用fail()方法指定的回调函数;如果执行状态是"未完成",则继续等待,或者调用progress()方法指定的回调函数(jQuery1.7版本添加)。

4.deferred 'then','always'

to glue 'Done' and 'Fail',use 'then' function

$.when($.ajax( "/main.php" ))

.then(successFunc, failureFunc ); OR .then(successFunc);
to always execute function,use 'always'

$.ajax( "test.html" )

.always( function() { alert("已执行!");} );
http://www.infoq.com/cn/news/2011/09/js-promise http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: