問(wèn)題描述
function1 = function(){
something.on('transitionend', function(){
// now function2 should run
});
}
function2 = function(){
alert('ok');
}
function1();
function2();
所以我聽(tīng)說(shuō)了 jQuery 承諾.我將返回一個(gè)延遲"對(duì)象,并在事件處理程序中調(diào)用 deferred.resolve();
So I heard about jQuery promises. I would return a "deferred" object, and inside the event handler I would call deferred.resolve();
但是如果我在那里有多個(gè)事件處理程序并且我只想在所有事件都被觸發(fā)后運(yùn)行下一個(gè)函數(shù)會(huì)發(fā)生什么?+ 我不喜歡在代碼的其他部分引入諸如延遲"之類(lèi)的外來(lái)內(nèi)容.
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.
有沒(méi)有其他方法可以檢測(cè) function1 是否完成了所有工作?
Is there any other way to detect if function1 has finished all its work?
推薦答案
試試這個(gè),
$.when($.ajax(fuction1())).then(function () {
fuction2;
});
這里 fuction1 是您要調(diào)用的第一個(gè)函數(shù),而 fuction2 是您要調(diào)用的第二個(gè)函數(shù).
Here fuction1 is your first function to call, and fuction2 is your second function.
這篇關(guān)于另一個(gè)完成后運(yùn)行函數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!