Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 325
salar Avatar asked Apr 08 '26 00:04

salar


1 Answers

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);
    }
});

like image 51
Rinrub Avatar answered Apr 09 '26 15:04

Rinrub