另一个完成后运行函数 [英] Run function after another one completes

查看:31
本文介绍了另一个完成后运行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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();

但是如果我在那里有多个事件处理程序并且我只想在所有事件都被触发后运行下一个函数会发生什么?+ 我不喜欢在代码的其他部分引入诸如延迟"之类的外来内容.

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 是您要调用的第一个函数,而 fuction2 是您要调用的第二个函数.

Here fuction1 is your first function to call, and fuction2 is your second function.

这篇关于另一个完成后运行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆