salar
salar

Reputation: 709

ajax loader does not display

I have jquery Ajax that work correctly ,

How can show message when ajax is calling method ?

i did some way but it does not work correctly .

  function drawChart(posId) {
        $.ajax({
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json',
            url: '@Url.Action("GetProviderReport", "Report")',
            data: { posId: posId },
            beforeSend: function () { $("#loaderDiv").css("display","block"); },
            success: function(result) {
                var data1 = new google.visualization.DataTable();
                var data2 = new google.visualization.DataTable();
                var data3 = new google.visualization.DataTable();
                var months = ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'ابان', 'اذر', 'دی', 'بهمن', 'اسفند'];

                data1.addColumn('string', 'ماه');
                data1.addColumn('number', 'مجموع نظر سنجی');

                for (var i = 0; i < result.length; i++) {
                    if (result[i].typeValue == 1) {
                        data1.addRow([months[result[i].MonthValue - 1], result[i].CountValue]);

                    }


                }

                var chart = new google.visualization.ColumnChart(document.getElementById('chartdiv7'));

And this is HTML Tag <div id="loaderDiv" style="visibility: hidden">در حال دریافت اطلاعات</div>

Upvotes: 3

Views: 176

Answers (2)

Rinrub
Rinrub

Reputation: 127

If this is a click event, you can add the loader on when the user clicks the button( $("#loaderDiv").css("display","block");) and then remove the loader when the get call is completed ( $("#loaderDiv").css("display","none");)

<div>
<button id="btn">Click me</button>

<script type="text/javascript">
$(function(){
    $("#btn").click(function(event) {
        clicked();
    });
    var clicked = function(){
        alert("add loader");
            //use the ajax call instead of setTimeout
        setTimeout(function() {
            //Remove the loader inside success/complete function
            alert("remove loader")
        }, 2000);
    }
});

Upvotes: 1

Craicerjack
Craicerjack

Reputation: 6332

if the ajax call is the only thing in this function then you could move the css part to the first part of the function then in the success/error part hide it again - needs to be in both incase the ajax fails.

function drawChart(posId) {
        $('#loaderDiv').css('visibility', 'visible');
        //OR
        $('#loaderDiv').show();
        $.ajax({
            type: 'GET',
            dataType: 'json',
            contentType: 'application/json',
            url: '@Url.Action("GetProviderReport", "Report")',
            data: { posId: posId },
            success: function(result) {
                $('#loaderDiv').css('visibility', 'hidden');
                //OR
                $('#loaderDiv').hide();

Upvotes: 0

Related Questions