Lingering objects for jquery

Source: Internet
Author: User

Before looking at someone else's demo, it is found that the code to be executed when the delay object is resolve is sometimes written in the Deferred.then method and sometimes written in the Deferred.done method. This makes me very confused about the lingering object smattering, today I took the time to study the next, and found that: in some environment, two methods do achieve the same effect. What about this particular environment? Let's take a look at the usage of Deferred.done:
1 //创建deferred对象<br>var dtd = $.Deferred();<br><br>//解决deferred对象<br>dtd.resolve(‘finish‘);
123 //调用done方法<br>dtd.done(doneCallback [, doneCallback])// 当deferred对象被 resolve 时,执行doneCallback函数<br>// 参数可为一个函数、多个函数或函数数组// 返回原来的deferred或promise对象
  Look at the usage and characteristics of Deferred.then: 
123456 //Create deferred object < Br>var DTD = $. Deferred (); <br><br>//resolves Deferred object <br>dtd.resolve (' Finish '); <br><br>//call then method <br>deferred.then (Donefilter [, Failfilter] [, Progressfilter]) <br>//then method attribute: //When deferred object is resolve, execute Donefilter function <br>//when deferred object is reject, execute Failfilter function <br >//when the Dederred object is progress, execute the Progressfilter function //return value: 1, Returns the Promise object of the deferred, which modifies the value passed by the Promise (the original Resolve,reject return value is a, a is modified to B, and B is returned, and the return value received by the promise's done or fail is changed to B); <br>/ /return Value: 2, create a new deferred object within the then method and return its promise //The returned Promise object can link other lingering objects, Asynchronous execution (one by one) >// //This method will filter out deferred modify the state of the method, the return value deferred object promise

Based on the characteristics of the above two methods, it is found that:

Both the Deferred.then and Deferred.done methods can receive a parameter function directly, and the first parameter function is called when the deferred object is resolve.

Although the then method can change the return value, the two methods do achieve the same effect without considering the return value and having only one parameter function.

By contrast, the done method is more pure, then the then method will be more complex, but not completely replace the Do method, using the then method, it is better to be careful.

Additional methods for attaching deferred objects:

To create a lingering object:
1 vardtd = $.Deferred();

Returns the current state of the lingering object

123 varstate = dtd.state();// 返回deferred对象当前状态,pending / resolved / rejected// 不接受任何参数

123 deferred.always( alwaysCallback [, alwaysCallback] );     // 当deferred对象被解决或拒绝时,都执行此方法     // 参数可以是一个函数,或是一个函数数组

  

123 dtd.promise( [obj] );     //目的: 防止其他代码干涉其内部进度和状态     //返回新的promise对象,包含可以执行的方法( done, fail, then, always, progress, state, promise ),不包含修改Deferred状态的方法( resolve, reject, notify, resolveWith, rejectWith, nodifyWith )

  

1234567 dtd.resolve( [args] )// 解决deferred对象,调用所有doneCallback函数// doneCallback可通过then方法中第一个参数设置,也可通过dtd.done( doneCallback )添加// 参数将传递给doneCallback。参数可选// 只有deferred对象的创建者才可以调用的方法// doneCallback中this为deferred或promise对象// doneCallback只接收一个参数

  

123456 dtd.resolveWith( context [,args] )// 解决deferred对象,调用所有doneCallback函数// 参数:第一个参数为上下文即this对象,doneCallback的this将被修改;第二个参数为数组// doneCallback中this为调用resolveWith方法的上下文// doneCallback接收参数个数为该方法第二个参数数组的长度// 与resolve方法的区别在于,将改变doneCallback函数的this指向

  

1234567 dtd.reject( [args] )// 拒绝deferred对象,调用所有failCallback函数// failCallback可通过then方法中第二个参数设置,也可通过dtd.fail( failCallback )添加// 参数将传递给failCallback。参数可选// 只有deferred对象的创建者才可以调用的方法// failCallback中this为deferred或promise对象// failCallback只接收一个参数

  

123456 dtd.rejectWith(context,  [args] )// 解决deferred对象,调用所有failCallback函数//  参数:第一个参数为上下文即this对象,failCallback的this将被修改;第二个参数为数组// failCallback中this为调用rejectWith方法的上下文// failCallback接收参数个数为该方法第二个参数数组的长度// 与resolve方法的区别在于,将改变failCallback函数的this指向

  

123456 dtd.notify( [args] )// deferred进行处理时,调用所有的progressCallback函数// progressCallback可通过then方法中的第3个参数设置,也可以通过deferred.progress( progressCallback )添加// 通常此方法只能被deferred对象的创建者调用,可通过deferred.promise或then过滤此方法// 参数可不写。若写有参数,建议为字符串或可返回字符串的函数// 当deferred进入 resolved 或rejected状态后,再调用notify方法,progressCallback将不再被执行

  

1234567 dtd.notifyWith(context,  [args] )// deferred进行处理时, 调用所有progressCallback函数//  参数:第一个参数为上下文即this对象,progressCallback的this将被修改;第二个参数为数组// progressCallback中this为调用rejectWith方法的上下文// progressCallback接收参数个数为该方法第二个参数数组的长度// 与resolve方法的区别在于,将改变progressCallback函数的this指向// 当deferred进入 resolved 或rejected状态后,再调用notifyWith方法,progressCallback将不再被执行

Lingering objects for jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.