Home > Web Front-end > CSS Tutorial > How to Display a Loading Indicator During Asynchronous AJAX Requests?

How to Display a Loading Indicator During Asynchronous AJAX Requests?

DDD
Release: 2024-11-17 05:50:03
Original
656 people have browsed it

How to Display a Loading Indicator During Asynchronous AJAX Requests?

Displaying Loading Indicator During Asynchronous Requests

To indicate the progress of asynchronous requests, a loading image can be displayed. While the request is being executed, the image will remain visible and disappear upon completion.

Using the Pre-Request and Post-Request Hiding/Showing Method

A straightforward approach is to manually control the image's visibility:

$('#loading-image').show();

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

Using Global Ajax Events

Alternatively, you can use global Ajax events to handle the image's visibility:

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

This method ensures that the loading image is displayed uniformly for all asynchronous requests.

The above is the detailed content of How to Display a Loading Indicator During Asynchronous 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template