2010-07-30 42 views

回答

3

不幸的是有沒有辦法做你以後,不是這樣的,你必須設置一個全局變量或一些其他的一些途徑。如果你看看how the jQuery success function works

handleSuccess: function(s, xhr, status, data) { 
    // If a local callback was specified, fire it and pass it the data 
    if (s.success) { 
    s.success.call(s.context, data, status, xhr); 
    } 

    // Fire the global callback 
    if (s.global) { 
    jQuery.ajax.triggerGlobal(s, "ajaxSuccess", [xhr, s]); 
    } 
} 

你可以看到它只是排序的問題,當地success處理總是發生前的全球事件被激發,這將觸發任何$.ajaxSuccess()處理程序。做你追求的最簡單的方法是調用任何功能,可以根據當地的success處理程序的開始取消,就像這樣:

$.ajax({ 
    //options... 
    success: function(data) { 
    if(shouldIBeCanceled()) return; 
    //rest of code... 
    } 
}); 

另一種方法是編輯的jQuery核心,或延長它,這將一定程度上產生插件的不可預知的行爲,所以我不會沿着這條路線走下去,因爲它在升級時也可能會帶來新的痛苦。