在javascript中,当第一个函数准备好时执行一个函数
在javascript中,有什么方法可以在第一个函数“准备好”时调用其他函数,
如下所示:
ridiculousTimeConsumingFunction().onReady( newFunction() );
为了说明我的示例,您可以看看她: http://web.cinaird.se/pdf/test.htm
Is there any way, in javascript, to call on a other function when the first function is "ready"
something like this:
ridiculousTimeConsumingFunction().onReady( newFunction() );
To illustrate my example you can take a look her:
http://web.cinaird.se/pdf/test.htm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
<罢工>
可笑的时间消耗函数();
newFunction();
将在
ridiculousTimeConsumingFunction()
完成后执行newFunction()
。您还可以执行类似
ridiculousTimeConsumingFunction(newFunction);
的操作,并按如下方式定义荒谬的TimeConsumingFunction:这将具有相同的效果。
事实上,放弃所有这些,因为它是一个异步事件,而不是一个耗时的函数...您必须使用回调:
然后按如下方式调用它:
ridiculousTimeConsumingFunction();
newFunction();
Will execute
newFunction()
afterridiculousTimeConsumingFunction()
has finished.You could also do something like
ridiculousTimeConsumingFunction(newFunction);
, and have ridiculousTimeConsumingFunction defined as follows:Which would have the same effect.
Infact, scrap all that, because it's an asynchronous event, not a time consuming function... You'll have to use a callback:
Then call it as follows:
“就绪”的概念通常不是为像您的示例这样的异步函数定义的。通常这种事情是通过回调来完成的:
The concept of "ready" is not generally defined for asynchronous functions like your example. Usually this kind of thing is done through callbacks:
在您的示例中,“ridiculousTimeConsumingFunction”函数实际上并不需要那么长时间来执行:它只是安排另一个函数在未来运行 1 秒。
如果你想做这样的事情,我建议你查看 jQuery UI 的效果 API。它允许您将动画一个接一个地“链接”在一起,并且应该达到您想要的效果。
In your sample, the "ridiculousTimeConsumingFunction" function doesn't actually take all that long to execute: it just scheduled another function to run 1 second in the future.
If you want to do something like this, I would suggest you check out jQuery UI's effects API. It allows you to "chain" animations together one after the other and should achieve the effect you're after.
如果您的意思是 ready 当 DOM 准备好时,则没有此类事件,但是您可以使用 jQuery 的
ready
处理程序在 DOM 准备就绪时触发您的函数。使用 Javascript,您也可以在
load
事件中触发您的函数,如下所示:或者
If you mean by ready when DOM is ready, there is no such event, however you can use jQuery's
ready
handler to fire your function when DOM becomes ready.With Javascript you could fire your function in
load
event too like this:Or