How to Show Loading Spinner in jQuery

  49362 views   2 years ago jQuery

Use the ajaxStart() and ajaxStop() Method

While working with Ajax, showing a loading spinner or displaying a message with some animation like "Loading... Please Wait" is popular way to indicate the user that Ajax request is in progress.

You can create a preloader using the jQuery ajaxStart() and ajaxStop() method.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Create a "Please Wait, Loading..." Animation</title>
    display: none;
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 999;
    background: rgba(255,255,255,0.8) url("loader.gif") center no-repeat;
/* Turn off scrollbar when body element has the loading class */
    overflow: hidden;   
/* Make spinner image visible when body element has the loading class */
body.loading .overlay{
    display: block;
<script src=""></script>
// Initiate an Ajax request on button click
$(document).on("click", "button", function(){
    $.get("customers.php", function(data){
// Add remove loading class on body element based on Ajax request status
    ajaxStart: function(){
    ajaxStop: function(){ 
<body style="text-align: center;">
    <button type="button">Get Customers Details</button>
    <p>Click the above button to get the customers details from the web server via Ajax.</p>
    <div class="overlay"></div>


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]