Search code examples
javascriptjquerybootstrap-4spinner

Bootstrap Spinner wont stop after form submitted and API call compete


I am using bootstrap and I am trying to setup a spinner when a form is submitted. I am able to get the spinner button to spin when the form is submitted however after it is submitted it just keeps spinning. I have looked all over but can't seem to figure out how to get this to stop once the API call is complete. How can i get this to spin when form is submitted, stop once API call is done and return back to usable button again after API call is complete?

Spinner Wont Stop

html Code

<form id="form_id" name="form_id" class="form_id" method="POST" >
    <div class="input-group-append">
        <button type="submit" id="btnFetch" class="btn btn-primary mb-2">Submit</button>
    </div>
</form>

JS Code

$(document).ready(function() {
    $("#btnFetch").click(function() {
        $(this).prop("disabled", true);
        $(this).html(
            `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Loading...`
        );
    });
});

Ajax call

$(document).ready(function() {
    $('.form_class1').on('submit', function(event) {
        $.ajax({
            data : {
                some_id: $('#some_id').val(),
            },
            type : 'POST',
            url : '/ajax_request_url1'

        })
        .done(function(data) {

            if (data.error) {

            }
            else {
                // Do Processing here....
            }

        });

        event.preventDefault();
        event.stopImmediatePropagation();

    });

});

Solution

  • You might consider reorganizing things a bit to simplify what's happening.

    1. Create individual functions to handle the spinner states.
    function startSpinner() {
        // your code to make the spinner start
        $("#btnFetch").prop("disabled", true);
        $("#btnFetch").html(
            `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Loading...`
        );
    }
    
    function stopSpinner() {
        // you code to make the spinner stop
        // (i.e., return the button to its original state)
        $("#btnFetch").prop("disabled", false);
        $("#btnFetch").html('Submit');
    }
    
    1. Call your spinner functions at appropriate times during the submission process.
    $(document).ready(function() {
        $('.form_class1').on('submit', function(event) {
    
            // initiate spinner
            startSpinner();
    
            $.ajax({
                data : {
                    some_id: $('#some_id').val(),
                },
                type : 'POST',
                url : '/ajax_request_url1'
            })
            .done(function(data) {
                // ...
            })
            .always(function(dataOrjqXHR, textStatus, jqXHRorErrorThrown) {           
                // do when complete, even on error            
                // stop the spinner
                stopSpinner();
            });
    
            // should these be at the top?
            event.preventDefault();
            event.stopImmediatePropagation();
    
        });
    
    });