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

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

问题描述

function1 = function(){

  something.on('transitionend', function(){
    // now function2 should run
  });

}

function2 = function(){
  alert('ok');
}

function1();
function2();

所以我听说了jQuery Promise.我将返回一个"deferred"对象,然后在事件处理程序中调用deferred.resolve();

So I heard about jQuery promises. I would return a "deferred" object, and inside the event handler I would call deferred.resolve();

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

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天全站免登陆