2013-01-14 19 views
0

我有幾個div了與Ajax調用(這是MVC,但HTML看起來是這樣的,當它完成)更新:JQuery的組合每個使用Live

<div class="progresswrapper running" id = "ProgressWrapper1"></div> 
<div class="progresswrapper running" id = "ProgressWrapper2"></div> 

隨後的javascript:

<script type="text/javascript"> 
    $(function() { 

var timerid = window.setInterval(function() { 
      $('.progresswrapper.running').each(function() { 
       UpdateDivWithDataFromServer(); 
       if(I'm done with this div) { 
        $(this).removeClass("running"); 
       } 
      }); 
     }, 500); 

是否有可能以某種方式做到這一點?

+0

使用[。對(http://api.jquery.com/on/),活已被棄用。 –

+0

如果您希望我們解決錯誤,則必須提供'UpdateDivWithDataFromServer',removeClass必須在ajax調用的oncomplete方法中運行。 – mattmanser

+0

除了使用加載內容或全局ajax事件的ajax請求的成功回調之外,您當前的代碼幾乎是唯一的方法。 '.live'和'.on'只對事件做出反應。使用ajax成功是處理它的最有效的方法。 –

回答

2
var timerid = window.setInterval(function() { 
$('.progresswrapper.running').each(function() { 
    UpdateDivWithDataFromServer(); 
}); 
}, 500); 

$(document).on("ajaxComplete",function(){ 
    $('.progresswrapper.running').each(function() { 
     if(I'm done with this div) { 
      $(this).removeClass("running"); 
     } 
    }); 
}); 
1

您的if()語句需要放在AJAX成功回調中,或者在.ajaxStop()處理程序中。

var timerid = window.setInterval(function() { 
     $('.progresswrapper.running').each(function() { 
      UpdateDivWithDataFromServer(); 
     }); 
    }, 500); 
$('.progresswrapper').ajaxStop(function() { 
     $(this).removeClass("running"); 
});