在事件中中止ajax调用

时间:2014-02-03 19:08:20

标签: jquery ajax

     $(document).ready(function(){

$(".Personalized").click(function(){



    if($("#divPersonalized").is(':visible')){

        $('#triangle-personalized').hide();
        $("#divPersonalized").hide();
    }

    else if($('#loading_personalized').is(':visible'))
    {
        $('#loading_personalized').hide();
                   //if this event is true, abort ajax call here

    }

    else {


    $.ajax({

        type:"POST",
        url:"personalized.php",
        cache:false,
        beforeSend: function(){
            $('#loading_personalized').show();
            $('#triangle-personalized').show();
        },

        complete: function(){
            $('#loading_personalized').hide();

        },
        success: function(html){


            $("#divPersonalized").html(html).show();
        }


    });
    }       
});

1 个答案:

答案 0 :(得分:1)

在变量说myAjaxCall中分配ajax调用,然后使用myAjaxCall.abort();

$(document).ready(function(){
   $(".Personalized").click(function(){
     if($("#divPersonalized").is(':visible')){
        $('#triangle-personalized').hide();
        $("#divPersonalized").hide();
     } else if($('#loading_personalized').is(':visible')){
        $('#loading_personalized').hide();
        //if this event is true, abort ajax call here
         myAjaxCall.abort();
     } else {
         myAjaxCall =  $.ajax({
                 type:"POST",
                 url:"personalized.php",
                 cache:false,
                 beforeSend: function(){
                 $('#loading_personalized').show();
                 $('#triangle-personalized').show();
                 },

                 complete: function(){
                     $('#loading_personalized').hide();
                 },
                 success: function(html){
                     $("#divPersonalized").html(html).show();
                 }
            });
      } 
     });
});