How to show a loading spinner using jQuery

Print Friendly, PDF & Email

Have you ever asked yourself how to show a spinner on a page while we have to wait for the server response? In most modern web-based applications, showing an indicator to tell our users when their request is finished is very valuable in the sense of usability as well as aesthetic appeal. You won’t have to wait for any longer. We are giving you a tutorial now.


As being similar to any HTML document, a loading spinner is just a GIF image. You could create a loading icon by using CSS as instructed in HowTo – CSS Loader on w3school. For a simple illustration about displaying a loading icon, I am using an image. You won’t get any difficult to get a loading image without any charge from the internet. For example,,, etc. Below is the image being embedded into the HTML page.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Show loading spinner</title>
    <style type="text/css">
        #loading {
            display: none;
<h2>Show loading spinner</h2>
<button type="button" value="Search" id="btnSearch">Search</button>

<div id="container">
    <img src="loading.gif" id="loading" title="working..." />
<script src=""

The page has only one button to perform a request where it hits a remote service to search all curated models from BioModels. What we expect to see is a spinner for some seconds after clicking on that button. It will disappear when the results are shown.

jQuery Code

Triggering the display of the loading spinner

With ajaxStart and ajaxStop, we can turn on or off the loader. In this example, we do nothing in .ajaxStart event but hide the spinner inside .ajaxStop meaning that we have received all data returned from the server.

<script type="text/javascript">
    const $loading = $('#loading');
        .ajaxStart(function () {

        .ajaxStop(function () {

Implementing actions on the click event

Please have an insight into my code below. There are some different ways of sending an ajax call to the server. We could use either get or fetch, except for ajax explicitly.

<script type="text/javascript">
    const $loading = $('#loading');
        .ajaxStart(function () {

        .ajaxStop(function () {

    $('#btnSearch').on("click", function () {
        // Found the solution for overcoming 'Access-Control-Allow-Origin' issue
        // See
        let url = "",
              proxyUrl = '';
        url =
        // Ref:
        // jQuery cross domain ajax
        /*$.get(proxyUrl+url).done(function (data) {

        // using the Fetch API
        /*fetch(proxyUrl + url).then(function (response) {
            return response.json();
        }).then(function (json) {

        // using ajax call
            type: "GET",
            url: proxyUrl + url,
            dataType: "json",
            success: function (response) {
            error: function (jXHR) {
                console.log("Error: " + jXHR.statusText);


That’s it!

The source code is published here.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.