Home Web Front-end JS Tutorial Detailed explanation of jQuery.ajaxStop() function

Detailed explanation of jQuery.ajaxStop() function

Jul 03, 2017 am 11:01 AM
function Detailed explanation

ajaxStop()Function is used to bind the handler function for the ajaxStop event of the AJAX request.

This is a global AJAX event function, used to execute the bound event processing function when the ajaxStop event is triggered.

jQuery official documentation description: Whenever an AJAX request is completed (whether successful or failed), jQuery will check whether there are other active (unfinished) AJAX requests. If no other active AJAX requests are found in the process, jQuery will trigger the ajaxStop event. At this time, all event handling functions bound through the ajaxStop() function will be executed.

In short, when an AJAX request ends and there are no other active AJAX requests at this time, the request will trigger the ajaxStop event.

Generally speaking, when multiple AJAX requests are executed continuously, only the last completed AJAX request will trigger the ajaxStop event. After all AJAX requests are executed, multiple AJAX requests are executed continuously again, or only the last completed AJAX request will trigger the ajaxStop event.

If an AJAX request is prohibited from triggering global AJAX events, it will not be considered an active AJAX request.

This function must be called on the jQuery object instance, and ajaxStop() will bind an event handler to each matching element. When the ajaxStop event is triggered, the handler functions bound to all matching elements will be called. This within the event handler will point to the current DOM element.

You can call this function multiple times for the same element to bind multiple event handlers. When an event is triggered, jQuery will execute the bound event processing functions in the order of binding.

Starting from jQuery 1.8, this function can only bind event handlers to document objects, and event handlers bound to other elements will not work.

If you set the option parameter global to false in jQuery.ajax() or jQuery.ajaxSetup(), you can prevent the AJAX request from triggering global AJAX events.

This function belongs to the jQuery object (instance).

Syntax

This function is new in jQuery 1.0.

jQueryObject.ajaxStop( handler )

Parameter

Parameter Description

handler Function type needs to be executed when this event is triggered event handling function.

Return value

ajaxStop()The return value of the function is of jQuery type and returns the current jQuery object itself.

Example & Description

For the ajaxStop event and the triggering mechanism of the ajaxStop event, you can refer to part of the source code of jQuery's jQuery.ajax() function.

Through the following source code, we can know: jQuery will count the number of currently active AJAX requests. Whenever an AJAX request is started, the active number is increased by 1; whenever an AJAX request is completed, the active number is decremented by 1. If the active number is 0 when an AJAX request starts, the ajaxStart event is triggered; if the active number is 0 when an AJAX request ends, the ajaxStop event is triggered.

// jQuery.ajax()函数的开头部分
var fireGlobals = s.global; // 是否允许触发全局AJAX事件
// 如果允许触发全局AJAX事件,并且活跃的AJAX请求数为0,则触发ajaxStart事件
if ( fireGlobals && jQuery.active++ === 0 ) {
    jQuery.event.trigger("ajaxStart");
}
// ... 省略中间的源代码
// jQuery.ajax()函数的末尾部分
if ( fireGlobals ) {
    globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] );
    // 如果允许触发全局事件,并且活跃的AJAX请求数为0,则触发ajaxStop事件
    if ( !( --jQuery.active ) ) {
        jQuery.event.trigger("ajaxStop");
    }
}
Copy after login

Please refer to the following HTML sample code:

<div id="content1">CodePlayer</div>
<div id="content2">专注于编程开发技术分享</div>
<div id="content3">http://www.365mini.com</div>
<input id="btn" type="button" value="开始AJAX请求" />
Copy after login

The following is the jQuery sample code related to the ajaxStop() function to demonstrate the specific usage of the ajaxStop() function:

Please note: The AJAX request that is executed first will not necessarily end first. This is closely related to the network, data volume, and related code execution time during the entire AJAX request process. The sample code below assumes that all AJAX requests take the same amount of time for ease of explanation.

Please run the following code based on jQuery versions prior to 1.8.

var $divs = $("div");
$divs.ajaxStop( function(){
    alert("处理函数1:当前元素id为" + this.id );
} );
$divs.ajaxStop( function(){
    alert("处理函数2:当前元素id为" + this.id );
} );
// 点击执行AJAX请求
$("#btn").click( function(){
    // 该AJAX请求结束时,下一个AJAX请求正在执行,因此不会触发ajaxStop事件,也不会弹出对话框   
    $.ajax( { url: "index.html" } );    
    
    // 该AJAX请求结束时,上一个AJAX请求已经执行完毕,因此会触发ajaxStop事件
    // 它会弹出6次对话框 ,因为当前页面有3个div元素,我们为每个div元素绑定了2个事件处理函数
    $.ajax( { url: "myurl.php" } );
    
} );
Copy after login

If the current jQuery is version 1.8 and above, the above jQuery code will not pop up the dialog box. Because starting from jQuery 1.8, the handler function of the ajaxStop event must be bound to the document object to take effect.

Therefore, regardless of the current jQuery version, if there are no special needs, we should bind the ajaxStop event handler to the document object.

var $divs = $("div");
$divs.ajaxStop( function(){
    alert("处理函数1");
} );
$divs.ajaxStop( function(){
    alert("处理函数2");
} );
// 点击执行AJAX请求
$("#btn").click( function(){
    // 该AJAX请求结束时,下一个AJAX请求正在执行,因此不会触发ajaxStop事件,也不会弹出对话框   
    $.ajax( { url: "index.html" } );    
    
    // 该AJAX请求结束时,上一个AJAX请求已经执行完毕,因此会触发ajaxStop事件
    // 它会弹出2次对话框 ,因为我们为document绑定了2个事件处理函数
    $.ajax( { url: "myurl.php" } );
    
} );
Copy after login

Now, we refer to the following jQuery code. We will delay the execution of the second AJAX request for 3 seconds (or other time value that can ensure the completion of the first AJAX request). At this time, both AJAX requests will pop up dialog boxes.

var $doc = $( document );
$doc.ajaxStop( function(){
    alert("处理函数1" );
} );
$doc.ajaxStop( function(){
    alert("处理函数2" );
} );
// 点击执行AJAX请求
$("#btn").click( function(){
    // 该AJAX请求结束时,此时没有其他活跃的AJAX请求(下一个请求尚未执行),因此会触发ajaxStop事件
    // 它会弹出2次对话框
    $.ajax( { url: "index.html" } );
    // 延迟3秒执行第二个AJAX请求
    setTimeout( function(){
        
        // 该AJAX请求结束时,此时没有其他活跃的AJAX请求(上一个请求早已经执行完毕)
        // 因此会触发ajaxStop事件,会弹出2次对话框
        $.ajax( { url: "myurl.php" } );
        
    }, 3000);   
    
} );
Copy after login

If the second AJAX request is prohibited from triggering global AJAX events, it will not be considered an active AJAX request. The first AJAX request triggers the ajaxStop event.

var $doc = $( document );
$doc.ajaxStop( function(){
    alert("处理函数1" );
} );
$doc.ajaxStop( function(){
    alert("处理函数2" );
} );
// 点击执行AJAX请求
$("#btn").click( function(){
    // 该AJAX请求开始时,此时没有其他活跃的AJAX请求(下一个请求虽然尚未执行完成,但它被禁止触发全局AJAX事件,jQuery在检查活跃请求时会忽略掉它)
    // 因此会触发ajaxStop事件,会弹出2次对话框
    $.ajax( { url: "index.html" } );
    
    // 该AJAX请求开始时,此时没有其他活跃的AJAX请求,但它不会触发ajaxStop事件
    // 因为它的global选项为false,被禁止触发任何AJAX事件,所以它不会弹出对话框
    $.ajax( { 
        url: "myurl.php" ,
        global: false // 禁止触发全局AJAX事件
    } );
    
} );
Copy after login

The above is the detailed content of Detailed explanation of jQuery.ajaxStop() function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

See all articles