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
DDDOriginal
2024-11-17 05:50:03654browse

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

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

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!

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