search
HomeWeb Front-endJS TutorialDetailed explanation of jQuery.ajaxStop() function

Detailed explanation of jQuery.ajaxStop() function

Jul 03, 2017 am 11:01 AM
functionDetailed 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");
    }
}

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请求" />

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" } );
    
} );

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" } );
    
} );

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);   
    
} );

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事件
    } );
    
} );

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
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
Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)