jQuery always vs. done vs. fail vs. then

Home文章
 简介:分析此三者的关系

参考:https://codeplanet.io/jquery-always-done-fail-then/

  1. Always – 成功或者失败后调用
  2. Done – 成功后调用
  3. Fail – 失败后调用
  4. Then – 上述三者个一个简写

always

$.get( "www.yoursite.com/api" ).always(function() {
 console.log('this will run whether the $.get fails or succeeds');
});

 

done

$.get( "www.yoursite.com/api" ).done(function() {
 console.log('this will run if the $.get succeeds');
});

fail

$.get( "www.yoursite.com/api" ).fail(function() {
 console.log('this will run if the $.get fails');
});

then

$.get( "www.yoursite.com/api" ).then(
 function() {
 console.log('this will run if the $.get succeeds');
 }, function() {
 console.log('this will run if the $.get fails');
 }, function() {
 console.log('this will run if the deferred generates a progress update');
 }
);

 

实际应用中always有个问题,如果出现轻微的语法错误可能导致always失效。

例如 data.length 未定义这种小错误

 

 



评论
Login