在javascript中,当第一个函数准备好时执行一个函数

发布于 2024-08-31 03:53:18 字数 281 浏览 3 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唔猫 2024-09-07 03:53:18

<罢工>
可笑的时间消耗函数();
newFunction();

将在 ridiculousTimeConsumingFunction() 完成后执行 newFunction()

您还可以执行类似 ridiculousTimeConsumingFunction(newFunction); 的操作,并按如下方式定义荒谬的TimeConsumingFunction:

function ridiculousTimeConsumingFunction(callback) {
   for (var i=0;i<1000000000;i++) {

   };

   callback();
}

这将具有相同的效果。

事实上,放弃所有这些,因为它是一个异步事件,而不是一个耗时的函数...您必须使用回调:

function longFunction (callback) {
  setTimeout(function(){
    $(".theDiv").css('height', 200 );
    callback();
  }, 1000);
}

然后按如下方式调用它:

longFunction(function () {
      $("#info").html(info);
});


ridiculousTimeConsumingFunction();
newFunction();

Will execute newFunction() after ridiculousTimeConsumingFunction() has finished.

You could also do something like ridiculousTimeConsumingFunction(newFunction);, and have ridiculousTimeConsumingFunction defined as follows:

function ridiculousTimeConsumingFunction(callback) {
   for (var i=0;i<1000000000;i++) {

   };

   callback();
}

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:

function longFunction (callback) {
  setTimeout(function(){
    $(".theDiv").css('height', 200 );
    callback();
  }, 1000);
}

Then call it as follows:

longFunction(function () {
      $("#info").html(info);
});
猫七 2024-09-07 03:53:18

“就绪”的概念通常不是为像您的示例这样的异步函数定义的。通常这种事情是通过回调来完成的:

function asyncFunc(callback) {
  setTimeout(function() {
    doSomething();
    callback();
  }, 1000);
}

asyncFunc(function() {alert('Done!');}

The concept of "ready" is not generally defined for asynchronous functions like your example. Usually this kind of thing is done through callbacks:

function asyncFunc(callback) {
  setTimeout(function() {
    doSomething();
    callback();
  }, 1000);
}

asyncFunc(function() {alert('Done!');}
温柔少女心 2024-09-07 03:53:18

在您的示例中,“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.

浅语花开 2024-09-07 03:53:18

如果您的意思是 ready 当 DOM 准备好时,则没有此类事件,但是您可以使用 jQuery 的 ready 处理程序在 DOM 准备就绪时触发您的函数。

使用 Javascript,您也可以在 load 事件中触发您的函数,如下所示:

window.onload = function(){
  // your function code here
};

或者

window.onload = somefunction;

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:

window.onload = function(){
  // your function code here
};

Or

window.onload = somefunction;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文