問題描述
function1 = function(){
something.on('transitionend', function(){
// now function2 should run
});
}
function2 = function(){
alert('ok');
}
function1();
function2();
所以我聽說了 jQuery 承諾.我將返回一個延遲"對象,并在事件處理程序中調用 deferred.resolve();
So I heard about jQuery promises. I would return a "deferred" object, and inside the event handler I would call deferred.resolve();
但是如果我在那里有多個事件處理程序并且我只想在所有事件都被觸發(fā)后運行下一個函數(shù)會發(fā)生什么?+ 我不喜歡在代碼的其他部分引入諸如延遲"之類的外來內容.
But what happens if i have multiple event handlers there and I only want the next function to run when all have been fired? + I don't like the idea of introducing something foreign like "deferred" into other parts of the code.
有沒有其他方法可以檢測 function1 是否完成了所有工作?
Is there any other way to detect if function1 has finished all its work?
推薦答案
試試這個,
$.when($.ajax(fuction1())).then(function () {
fuction2;
});
這里 fuction1 是您要調用的第一個函數(shù),而 fuction2 是您要調用的第二個函數(shù).
Here fuction1 is your first function to call, and fuction2 is your second function.
這篇關于另一個完成后運行函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!