Home > Web Front-end > CSS Tutorial > How Can I Show a Loading Indicator During jQuery AJAX Requests?

How Can I Show a Loading Indicator During jQuery AJAX Requests?

Barbara Streisand
Release: 2024-11-18 01:42:01
Original
943 people have browsed it

How Can I Show a Loading Indicator During jQuery AJAX Requests?

Showing Loading Indication During $.ajax Requests

Question:

How can I display a visual indicator while an asynchronous HTTP request is being made using $.ajax?

Answer:

To indicate that an asynchronous request is being processed, you can utilize loading images. Here are two methods to do so:

Method 1: Inline Display Control

Show the loading image before the request and hide it after the request completes:

$('#loading-image').show();
$.ajax({
  url: uri,
  cache: false,
  success: function(html){
    $('.info').append(html);
  },
  complete: function(){
    $('#loading-image').hide();
  }
});
Copy after login

Method 2: Global Event Binding

Bind the loading image display to global ajaxStart and ajaxStop events. This approach will toggle the image for all ajax requests:

$('#loading-image').bind('ajaxStart', function(){
  $(this).show();
}).bind('ajaxStop', function(){
  $(this).hide();
});
Copy after login

The above is the detailed content of How Can I Show a Loading Indicator During jQuery AJAX Requests?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template