Search code examples
javascriptjqueryajaxshowshow-hide

Showing or hiding div not working with async false ajax calls


I have some functions that make some ajax calls.
In order to execute everything the way is needed, these requests must by set with async: false.

Everything is ok with the ajax call. My problem is that I need a div (a simple css loader) to be shown before send the request and hide after it, but it is not showing.

This is my function:

$("#btn1").on('click', function(){  
       // Apparently it does not executes this          
        $('.container-loader').show(); 
        //execute the function and handle the callback
        doSomeStuff(function(c){
            if(!c.ok){
                showModalWarning();
                $('#text').append('<li>'+c.msg+'</li>');
            }
            else{
                toastr.success('Everything is ok');
                doOtherStuff($("#select").val());               
            }
        });

        var modal = document.getElementById('Modal1');
        modal.style.display = "none";
        return false;
    });

My doSomeStuff() function that makes the requests:

function doSomeStuff(callback){     
            //...
            for (var i = 0; i < ids.length; i++) {
                var Id = ids[i][0];
                var ch = ids[i][1];
                var tp = 2;                             
                var url = 'http://domain.com.br:8080/datasnap/rest/TSM/Fun/' + tp + '/' + $("#select").val() + '/' + ch;
                $.ajax({
                    cache: "false",
                    async: false, //it needs to by with async false
                    dataType: 'json',
                    type: 'get',
                    url: url,
                    success: function(data) {
                        if (!data)
                            toastr.error('error' );
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        toastr.error("some problem");
                    }
                });
            }
            callback({ok: true});
    }

Any idea on how can I handle this? I am really new with async stuff.


Solution

  • Solved this by removing async and changing the mthod in my server to receive an array as a parameter.

    The final script:

    $("#btn1").on('click', function(){             
            //$('.container-loader').show(); 
            //execute the function and handle the callback
            doSomeStuff(function(c){
                if(!c.ok){
                    showModalWarning();
                    $('#text').append('<li>'+c.msg+'</li>');
                }
                else{
                    toastr.success('Everything is ok');
                    doOtherStuff($("#select").val());               
                }
            });
        });
    

    My doSomeStuff() function that makes the requests:

    function doSomeStuff(callback){     
                //...
                var tp = 2;                             
                var url = 'http://domain.com.br:8080/datasnap/rest/TSM/Fun/' + tp + '/' + $("#select").val() + '/' + encodeURIComponent(JSON.stringify(jsonArray));
                $.ajax({
                        cache: "false",
                        //async: false, //it needs to by with async false
                        dataType: 'json',
                        type: 'get',
                        url: url,
                        success: function(data) {
                            if (!data)
                             callback({ok: false});
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            toastr.error("some problem");
                        }, complete: function(){ //hide the loader after complete
                            $('.container-loader').hide();
                            var modal = document.getElementById('Modal1');
                            modal.style.display = "none";
                        }
                });                
        }