riya sharma
riya sharma

Reputation: 77

ajax call not taking place

After the animation is complete I make an ajax call as shown below, but nothing happens there. Ajax call doesn't happen at all. Nothing is sent to the php file processing ajax request. I don't understand what is wrong or what I have missed. Thanks in advance!!!

$('.open, .closed, .appoint').each(function (index) {

    var $control = $('#' + (firstDay + index));
    $control.animate({
        top: pos[firstDayNext + index].top,
        left: pos[firstDayNext + index].left + (($control.html() < 10) ? 5.7 : 0),
        }, (400 + (25 * index)));

}).promise().always(function(){

    $.ajax({
        type:"GET",
        url:"ajax/showingAppointmentForMonth.php",
        dataType:'JSON',
        data:"year=" + currentYear + "&month=" + currentMonthInteger + "&id=" + "<?php echo $id;?>",
        success:function(data){
            //do something with the data
        }
    });

});

Upvotes: 0

Views: 40

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

Because the animation is not happening in the iterated set of elements so the promise will not be invoked.

Instead use .map() and return the $control element and call the promise on that set of elements

var firstDay = '01-Aug-'

$('.open, .closed, .appoint').map(function(index) {
  var $control = $('#' + (firstDay + index));
  $control.animate({
    height: 'hide' //to demo
  }, (4000 + (25 * index)));
  return $control.get();
}).promise().always(function() {

  alert('ajax')

  //ajax here
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class="open">open</span>
<span class="closed">closed</span>
<span class="appoint">appoint</span>

<br />
<div id="01-Aug-0">01-Aug-1</div>
<div id="01-Aug-1">01-Aug-2</div>
<div id="01-Aug-2">01-Aug-3</div>

Upvotes: 1

Related Questions