2010-05-20 65 views

回答

6

你應該基本設置變量的腳本設置爲false和阿賈克斯頂部的啓動將其設置爲true並在success處理器再次設置爲falsedescribed here

+2

有時最好的解決方案是最簡單的:)謝謝! – bah 2010-05-20 09:15:20

+0

@bah:不客氣,同意:) – Sarfraz 2010-05-20 09:16:08

+0

嗨!我正在測試一個網頁,我想知道如何使用Java腳本知道Ajax請求何時完成(即,我無法控制網頁,我只能注入Java腳本)? – 2012-07-31 11:29:49

1

如果您完全控制了JavaScript代碼,那麼無論何時您啓動ajax請求並在請求完成時(遞交成功,失敗或超時)遞減該變量,都可以遞增變量。這樣你總能知道是否有請求正在進行。

10

是有

 

$.ajax({ 
    type: 'get', 
    url: 'url', 
    data: { 
       email: $email.val() 
    }, 
    dataType: 'text', 
    success: function(data) 
    { 
     if(data == '1') 
     { 
      $response.attr('style', '') 
        .attr('style', "color:red;") 
        .html('Email already registered please enter a different email.'); 
     } 
     else 
     { 
      $response.attr('style', '') 
        .attr('style', "color:green;") 
        .html('Available'); 
     } 
    }, 
    beforeSend: function(){ 
       $email.addClass('show_loading_in_right') 
    }, 
    complete: function(){ 
       $email.removeClass('show_loading_in_right') 
    } 
}); 
 


beforeSend將你需要的時候Ajax請求剛開始做,Ajax請求完成後完成將被調用的過程。
documentation

+0

我不認爲這解決了OP的問題。他在發送請求之前或者在請求完成之後不想知道如何觸發處理程序,他希望在請求正在進行時觸發某些事件* ... – SexyBeast 2013-02-11 10:08:46

4

你可能會喜歡這樣的:

$(document).ready(function() { 
    var working = false; 
    $("#contentLoading").ajaxSend(function(r, s) { 
     $(this).show(); 
     $("#ready").hide(); 
     working = true; 
    }); 

    $("#contentLoading").ajaxStop(function(r, s) { 
     $(this).hide(); 
     $("#ready").show(); 
     working = false; 
    }); 

    $('#form').submit(function() { 
     if (working) return; 
     $.post('/some/url', $(this).serialize(), function(data){ 
      alert(data); 
     }); 
    }); 
}); 
4

要放棄Ajax請求, 使用

if($.active > 0){ 
ajx.abort();//where ajx is ajax variable 
} 

要繼續運行Ajax請求, 使用

if($.active > 0){ 
return; 

} 
+0

我找不到任何有關$ .active的文檔。你能舉一些例子來說明這個功能嗎? – 2014-08-25 12:40:03

+0

@Ganesh,看看這些帖子:http://stackoverflow.com/questions/3148225/jquery-active-function。 – StartCoding 2014-08-27 05:37:52