楼主: ReneeBK
1380 6

Learning jQuery Deferreds [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4900份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49655 个
通用积分
55.9937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2015-10-18 03:52:09 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Learning deferred earning Jquery Query Media

本帖被以下文库推荐

沙发
ReneeBK 发表于 2015-10-18 03:53:31
  1. Query Deferreds
  2. We chose to focus on jQuery deferreds because jQuery is ubiquitous and because two important and familiar aspects of jQuery ($.ajax and animations) already have deferred support built in.

  3. However, jQuery deferreds are certainly not the last word on the subject. As you will see, they differ markedly in one important respect from the many (at least 35) other deferred packages for JavaScript: jQuery deferreds do not currently follow the excellent Promises/A+ specification (see Promises/A+).

  4. This book was written for the 1.10.2 or 2.0.3 versions of jQuery. Because the jQuery deferred API has changed several times, it is important to know where you stand, as we’ll see in Changes in the jQuery Deferred API. Server side, we’re using version 1.9.1 of the jQuery-deferred node module. The node module and much of the jQuery deferred code was written by Julian Aubourg.
复制代码

藤椅
ReneeBK 发表于 2015-10-18 03:56:08
  1. Creating Deferreds
  2. Once you’re comfortable receiving promises from functions and working with them by attaching callbacks, you’ll soon want to write your own function or API that creates a deferred and returns its promise.

  3. Construction
  4. To create a deferred, you just call $.Deferred():

  5. var deferred = $.Deferred();
  6. As discussed in Terminology: Deferreds and Promises, instead of returning a deferred to the caller of your code, you’ll almost always want to return a promise. The promise is obtained by calling the promise method on the deferred:

  7. var deferred = $.Deferred(),
  8.     promise = deferred.promise();
  9. For convenience, the $.Deferred constructor allows you to pass it a function that can be used to initialize the deferred once it is created. The function receives the deferred as its only argument. For example, here’s a handy succeed function[6] that returns a promise that has already been fired with a specified result:

  10. function succeed(value){
  11.     return $.Deferred(function(d){
  12.         d.resolve(value);
  13.     }).promise();
  14. }
复制代码

板凳
ReneeBK 发表于 2015-10-18 03:57:50
  1. Here’s a simple replacement for setTimeout that uses deferreds. This is adapted from Fun With jQuery Deferreds by Michael Bleigh.

  2. function wait(timeout){
  3.     var deferred = $.Deferred();
  4.     setTimeout(deferred.resolve, timeout);
  5.     return deferred.promise();
  6. }
  7. And here’s how you use it:

  8. wait(500).done(function(){
  9.     console.log('Timeout fired!');
  10. });
  11. This produces the following, as you would expect:

  12. Timeout fired!
复制代码

报纸
ReneeBK 发表于 2015-10-18 03:59:58

Messaging in Chrome Extensions

  1. With the sendMessageDeferred function in place, we can now use it in our code:

  2. sendMessageDeferred(17, { action: 'addSidebar' })
  3. .done(function(result){
  4.     if (result.success){
  5.         console.log('Sidebar added to DOM.');
  6.     }
  7.     else {
  8.         console.error('Could not add sidebar to tab 17:', result.error);
  9.     }
  10. })
  11. .fail(function(error){
  12.     console.error('Chrome error:', error);
  13. });
  14. We could use $.when to send messages to several tabs, only proceeding once all tabs had replied:

  15. $.when(
  16.     sendMessageDeferred(17, { action: 'addSidebar' }),
  17.     sendMessageDeferred(18, { action: 'hideSidebar' }),
  18.     sendMessageDeferred(19, { action: 'hideSidebar' })
  19. )
  20. .done(function(){
  21.     console.log('All tab sidebars adjusted.');
  22. })
  23. .fail(function(error){
  24.     console.error('Chrome error:', error);
  25. });
复制代码

地板
ReneeBK 发表于 2015-10-18 04:01:17

Accessing Chrome Local Storage

  1. To make a deferred method for set, we don’t really need to know what items represents. All we need to know is that callback will be called by Chrome when it has set the key values it is given. Our version of set will look as follows:

  2. function set(items){
  3.     var deferred = $.Deferred(); // 1
  4.     chrome.storage.local.set(items, function(){
  5.         if (chrome.runtime.lasterror){
  6.             deferred.reject(chrome.runtime.lasterror.message); // 2 }
  7.         else {
  8.             deferred.resolve(); // 3 }
  9.     });

  10.     return deferred.promise(); // 4 }
  11. This should look familiar by now. It follows the classic pattern:

  12. 1
  13. Create a deferred.

  14. 2
  15. Arrange to reject the deferred in case of error.

  16. 3
  17. Arrange to resolve the deferred with a result if no error occurs.

  18. 4
  19. Return a promise created from the deferred.

  20. You’d use the above set function as follows:

  21. set({
  22.     lastURL: 'http://bit.ly/jquery-deferreds',
  23.     timestamp: new Date().toUTCString()
  24. }).done(function(){
  25.     // Keys for the user's last URL and timestamp saved to local storage.
  26. });
复制代码

7
ReneeBK 发表于 2015-10-18 04:03:19

Running Promise-Returning Functions One by One

  1. function synchronously(tasks){
  2.     var i, task, func,
  3.         promise = $.Deferred().resolve().promise(), // 1 makeRunner = function(func, args){ // 2 return function(){
  4.                 return func.apply(null, args).promise();
  5.             };
  6.         };
  7.     for (i = 0; i < tasks.length; i++){
  8.         task = tasks[i];
  9.         func = task.shift();
  10.         promise = promise.then(makeRunner(func, task)); // 3 }
  11.     return promise;
  12. }
  13. This works as follows:

  14. 1
  15. Create a promise variable that we’ll eventually return. The deferred from which the promise is made is resolved immediately with an undefined value. When we first call then on that promise, the function we pass to then will be run immediately. Note that we don’t care about the value the promise fires with; we’re just trying to get the task functions run sequentially.

  16. 2
  17. makeRunner functions as a closure that returns a function to invoke the current task with its arguments. Note that we assume that calling func returns either a promise or something that can be used to obtain a promise.

  18. There is an unfortunate subtlety here. All promises have a promise method (undocumented!) that, when called with no arguments, returns the promise. The method is actually identical to the (documented) promise method of its deferred. So if func returns a promise, calling promise() on it simply returns the promise. More usefully, if func happens to be a call to animate, the call to promise() is essential—it ensures that the returned value, passed to then, is a promise. See the challenges below for more on this.

  19. 3
  20. This slightly dubious line reuses (updates) the promise variable to hold the promise returned by calling then on the current promise. In effect, we’re adding a new function to be called when the current promise is done and then forgetting about that promise. We only have to worry about the most recent promise, which we return to our caller. Because the original promise was fired on creation, we don’t have to hang onto it for any reason.
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-24 17:40