Analysis of the asynchronous mechanism of jQuery source code
This article mainly introduces the analysis of the asynchronous mechanism of jQuery source code. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
Asynchronous mechanism
JavaScript programming is often accompanied by asynchronous programming, such as remote data acquisition. A large amount of asynchronous programming will bring many callback functions. JS is single-threaded, so completing asynchronous often requires the help of browser event drivers, which will make our code and Algorithms become fragmented. jQuery provides an abstract non-blocking
solution: Deferred
.
Understanding asynchronous
alert(1) setTimeout(function(){ alert(2) },0) alert(3) //alert(1) //alert(3) //alert(2)
1. Understanding deferred
Objects
In the process of developing websites, we often encounter certain JavaScript operations that take a long time. Among them, there are both asynchronous operations (such as ajax reading server data)
and synchronous operations (such as traversing a large array)
. None of them can get the results immediately.
The usual approach is to specify callback functions (callback)
for them. That is, specify in advance which functions should be called once they have finished running.
deferred
The object was introduced in jquery version 1.5. jquery was not very good at handling callbacks, so the deferred object was born. The deferred
object is the callback function solution for jQuery
. In English, defer
means "delay", so the meaning of a deferred object is to "delay" execution until a certain point in the future.
2. Ajax chain operation
The initial writing method is:
// < 1.5 $.ajax({ url: "test.html", success: function(){ alert("哈哈,成功了!"); }, error:function(){ alert("出错啦!"); } });
If the version of jquery
is 1.5
or below , then the XHR object
is returned, so the chain operation cannot be performed. However, if the version is higher than 1.5, the Deferred
object is returned, and the chain operation can be performed.
// >=1.5 $.ajax("test.html") .done(function(){ }) .fail(function(){ })
done() is equivalent to the success method, and fail() is equivalent to the error method. After adopting the chain writing method, the readability of the code is greatly improved.
3. Specify multiple callback functions for the same operation
One of the great benefits of the deferred object is that it allows you to add multiple callback functions freely.
Still taking the above code as an example, if after the ajax operation is successful, in addition to the original callback function, I also want to run another callback function, what should I do?
$.ajax("test.html") .done(function(){ alert("哈哈,成功了!");} ) .fail(function(){ alert("出错啦!"); } ) .done(function(){ alert("第二个回调函数!");} );
4. Specify callback functions for multiple operations
Another great benefit of the deferred object is that it allows you to specify a callback function for multiple events, which is not possible with traditional writing. of. That is $.when()
$.when($.ajax("test1.html"), $.ajax("test2.html")) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); }); //$.ajax("test1.html")和$.ajax("test2.html"),如果都成功了,就运行done()指定的回调函数;如果有一个失败或都失败了,就执行fail()指定的回调函数。
5. Callback function interface extension for common operations
It extends this set of callback function interfaces from ajax operations to all operations. In other words, any operation - whether it is an ajax operation or a local operation, whether it is an asynchronous operation or a synchronous operation - can use various methods of the deferred object to specify a callback function.
// 一个耗时操作 var wait = function(){ var task = function(){ alert('执行了'); }; setTimeout(5000); }
When we add a callback, we may think of $.when();
$.when(wait()) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); });
But it has no effect. done is executed immediately because $.when() accepts a deferred object. So let’s improve it further
var dtd = $.Deferred(); var wait = function(){ var task = function(){ alert('执行了'); dtd.resolve(); // 改变deferred对象的执行状态 }; setTimeout(task, 5000); return dd; }
In this way, wait() returns a defferred object, which can be operated on the chain.
$.when(wait()) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); });
6. defferred.resolve() and defferred.reject()
deferred uses the principle of Promise. Here we need to be clear about the concept of execution status. In the defferred object, there are three states, Uncompleted
Completed
Failed
If the execution status is "Completed "
(resolved), the deferred object immediately calls the done() method
specified callback function;
If the execution status is "Failed"
, call fail ()
The callback function specified by the method;
If the execution status is "Not Completed"
, continue to wait, or call the callback function specified by the progress()
method ( Added in jQuery 1.7 version).
defferred.resolve()
is to change the status from Uncompleted
to Completed
. ==> done()defferred.reject()
changes the status from Uncompleted
to Failed
. ==> fail()
var dtd = $.Deferred(); // 新建一个Deferred对象 var wait = function(){ var tasks = function(){ alert("执行完毕!"); dtd.resolve(); // 改变Deferred对象的执行状态 }; setTimeout(tasks,5000); return dtd; // 返回promise对象 }; $.when(wait()) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); }); dtd.resolve();// 这里会立即先执行一次
7. defferred.promise();
The above method has some problems. For example, dtd is a global variable, and it can be used externally at any time. This edge state will cause done() or fail() to be executed more than once.
This is obviously not what we want, so we need the help of defferred.promise.
Its function is to return another deferred object on the original deferred object. The latter only opens methods unrelated to changing the execution status (such as done() method and fail() method), shielding and changing Execution status-related methods (such as resolve() method and reject() method), so that the execution status cannot be changed.
var wait = function(dtd){ var dtd = $.Deferred(); //在函数内部,新建一个Deferred对象 var tasks = function(){ alert("执行完毕!"); dtd.resolve(); // 改变Deferred对象的执行状态 }; setTimeout(tasks,5000); return dtd.promise(); // 返回promise对象 }; var dd = wait(); // dd.resolve(); // 这里如果使用了的话,就会报错的哦,因为返回deferred.promise()是无法对状态修改的哦 $.when(dd) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); });
8. $.Defferred(wait)
Another way to prevent execution status changes is to use $.Defferred()
var dtd = $.Deferred(); // 新建一个Deferred对象 var wait = function(dtd){ var tasks = function(){ alert("执行完毕!"); dtd.resolve(); // 改变Deferred对象的执行状态 }; setTimeout(tasks,5000); return dtd; }; var ddd = wait(); // dd.resolve(); //这里报错了哦 $.Deferred(dd) .done(function(){ alert("哈哈,成功了!"); }) .fail(function(){ alert("出错啦!"); });
9. Summary of defferred objects
$.Deferred() generates a deferred object.
deferred.done() specifies the callback function when the operation is successful
deferred.fail() 指定操作失败时的回调函数
deferred.promise() 没有参数时,返回一个新的deferred对象,该对象的运行状态无法被改变;接受参数时,作用为在参数对象上部署deferred接口。
deferred.resolve(args) 手动改变deferred对象的运行状态为"已完成",从而立即触发done()方法。解决Deferred(延迟)对象,并根据给定的args参数调用任何完成回调函数(doneCallbacks)。
deferred.resolveWith() 解决Deferred(延迟)对象,并根据给定的 context和args参数调用任何完成回调函数(doneCallbacks)。
deferred.reject(args) 这个方法与deferred.resolve()正好相反,调用后将deferred对象的运行状态变为"已失败",从而立即触发fail()方法。拒绝Deferred(延迟)对象,并根据给定的args参数调用任何失败回调函数(failCallbacks)。这里的args是一个对象。
deferred.rejectWith(context, args) 拒绝Deferred(延迟)对象,并根据给定的 context和args参数调用任何失败回调函数(failCallbacks)。这里的args是一个数组类型。
$.when() 为多个操作指定回调函数。除了这些方法以外,deferred对象还有二个重要方法,上面的教程中没有涉及到。Context(上下文) 作为 this对象传递给失败回调函数(failCallbacks )
deferred.then()
有时为了省事,可以把done()和fail()合在一起写,这就是then()方法。
$.when($.ajax( "/main.php" )) .then(successFunc, failureFunc );
如果then()有两个参数,那么第一个参数是done()方法的回调函数,第二个参数是fail()方法的回调方法。如果then()只有一个参数,那么等同于done()。
deferred.always() 这个方法也是用来指定回调函数的,它的作用是,不管调用的
deferred.resolve()
还`是deferred.reject(),最后总是执行。
$.ajax( "test.html" ) .always( function() { alert("已执行!");} );
deferred.state() 确定一个Deferred(延迟)对象的当前状态。
"pending": Deferred对象是尚未完成状态 (不是 "rejected" 或 "resolved").
"resolved": Deferred对象是在解决状态,这意味着,deferred.resolve() 或者 deferred.resolveWith()被对象访问和doneCallbacks被访问(或在被调用的过程中)
"rejected": Deferred对象是在被拒绝的状态,这意味着,deferred.reject() 或者 deferred.rejectWith() 被对象访问和failCallbacks被访问(或在被调用的过程中) 。
这种方法主要是用在调试,例如,在准备拒绝(reject)一个延迟对象前,判断它是否已经处于 resolved 状态。
十、使用实例
情景1:当用户按下删除弹窗的确定或取消后,把弹窗隐藏,并执行对应的操作(删除或不执行),因为我们不知道用户什么时候会点击按钮,所以不能让弹窗阻塞其他任务的执行。
function pop(arg) { if (!arg) { console.error('pop title is empty'); } var dfd = $.Deferred() //实例化一个延迟对象 , confirmed //记录按下确定或取消 , $confirm = $content.find('button.confirm') //确认按钮 , $cancel = $content.find('button.cancel'); //取消按钮 //定时器轮询,当按下确定或取消时触发删除或取消操作 timer = setInterval(function() { if (confirmed !== undifined) { dfd.resolve(confirmed); clearInterval(timer); dismiss_pop(); } }, 50); //点击确定时更改confirmed状态 $confirm.on('click', function() { confirmed = true; }); //点击取消时更改confirmed状态 $cancel.on('click', function() { confirmed = false; }); //返回dfd对象 return dfd.promise(); } $('.delete').click(function() { var $this = $(this); var index = $this.data('index'); //当前的id //确定删除 pop('确定删除?').then(function(res) { res ? delete_task(index) : null; }) })
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of Analysis of the asynchronous mechanism of jQuery source code. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
