如何只另一个函数完成后执行一个JavaScript

如何只另一个函数完成后执行一个JavaScript

本文介绍了如何只另一个函数完成后执行一个JavaScript / jQuery的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个简单的问题,但我真的不知道该怎么做。我执行两个功能中的我的code,'luxboxEngine'和'fitToScreen',后者需要前者的完成。我如何告诉fitToScreen'到'luxboxEngine后,只执行已完成?现在我有这个,它得到了预期的效果:

I know this is a simple question, but I don't really know how to do it. I'm executing two functions in my code, 'luxboxEngine' and 'fitToScreen', the latter of which requires the completion of the former. How do I tell 'fitToScreen' to only execute after 'luxboxEngine' has completed? Right now I have this, which gets the desired results:

luxboxEngine(self);
setTimeout(fitToScreen, 1000);

...但我知道,这不是一个很好的解决方案。我读过有关回调函数,但我怕我真的不明白他们是什么/如何将它们。谢谢阅读。

...But I know that's not a good solution. I've read about callback functions, but I'm afraid I don't really get what they are / how to incorporate them. Thanks for reading.

编辑:那么作为Sudharsan斯里兰卡和Brian McGinity下面回答,回调是解决方案。这里的code我用得到什么,我需要在我的程序:

So as Sudharsan Sri and Brian McGinity answered below, a callback is the solution. Here's the code I used to get what I need in my program:

    luxboxEngine(self, function () {
       fitToScreen();
    });

这luxboxEngine完成后触发fitToScreen功能。

That fires the fitToScreen function after the completion of luxboxEngine.

推荐答案

使用的回调:

luxboxEngine(self , function() { fitToScreen(); });

这篇关于如何只另一个函数完成后执行一个JavaScript / jQuery的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:17