使用外部等待类暂停 javascript 函数的执行

发布于 2024-11-17 23:53:31 字数 927 浏览 1 评论 0原文

我试图弄清楚如何在调用另一个函数之前暂停函数的执行,同时使我的 javascript 代码极具可读性。这是我想做的一个例子:

function main_function(){   
   function a();
   // wait for function a to finish - could take 1 second - could take 3 seconds

   function b();
   // wait for function b to finish - don't know how long this will take

   function c();
   // completed
}

我已经阅读了几十篇关于如何使用 setTimeOut、重定向到其他函数、回调等的文章,但无法找到真正有效的解决方案(而且大多数只是使用笨重的 setTimeout() ,这确实没有帮助,因为我不知道每个函数可能需要多长时间)。

所以,我终于遇到了一个名为“WaitThread.js”的小实用程序。这似乎正是我需要做的事情,而且似乎以后很容易阅读和维护。

但是,我不知道如何使用它! :)

有人能给我提供一个如何使用这个 WaitThread.js 的例子吗?或者至少为我提供一种可读/优雅的方式来等待 javascript 函数依次执行,同时等待每个函数先完成?

这里是 WaitThread.js 页面的链接:

http://www.robertmayo.com/blog/2006/07/htmljavascript-wait-for-asynchronous.html

谢谢!

I am trying to figure out how to pause execution of a function before calling another function while making my javascript code extremely readable. Here is an example of what I am trying to do:

function main_function(){   
   function a();
   // wait for function a to finish - could take 1 second - could take 3 seconds

   function b();
   // wait for function b to finish - don't know how long this will take

   function c();
   // completed
}

I have read dozens of articles on how to use setTimeOut, redirects to other functions, callbacks, etc. but was unable to find a solution that really worked well (and most of them just use a clunky setTimeout(), which really doesn't help since I have no idea how long each function might take).

So, I finally came across a small utility called "WaitThread.js". This seems to be exactly what I am needing to do and it seems like it would be something that is easy to read and maintain later on.

However, I can't figure out how to use it! :)

Would anyone be able to provide me an example of how to use this WaitThread.js? Or at least provide me with a readable/elegant way of waiting for javascript functions to execute one after another while waiting for each one to finish first?

Here is a link to the WaitThread.js page:

http://www.robertmayo.com/blog/2006/07/htmljavascript-wait-for-asynchronous.html

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雪若未夕 2024-11-24 23:53:31

根据waitthread.js的解释,它只是使用一个定时器来轮询,等待某个变量值的变化。

此类问题的更典型设计模式使用回调,并且函数 a 将在工作完成时调用回调,从而启动函数 b。因此,您将一个函数传递给function a,当function a完成其工作时应该调用该函数。更完整的设计模式通常有成功退出和不成功退出的回调,甚至可能从退出传递参数,但由于我不知道你的具体情况,所以我没有尝试在这里建模。对于多步骤过程,它看起来像这样。我们假设三个异步函数作为函数 a、b 和 c 存在,并且每个函数都采用一个函数作为参数,该函数将在异步函数完成时被调用:

function main_function_step1() {
  a(main_function_step2);
}

function main_function_step2() {
  // execute code here that goes after function a, but before function b
  b(main_function_step3);
}

function main_function_step3() {
  // execute code here that goes after function b, but before function c
  c(main_function_finish);
}

function main_function_finish()
{
  // execute whatever code here to finish
}

更完整的解决方案将传递一个带有成功回调和失败回调的对象,将为每个函数至少提供一个参数,以便可以返回结果或错误条件。

在这种情况下,它看起来像这样:

function main_function_step1() {
  function main_function_a_step1_fail(err)
  {
    // handle error in step 1
  }
  var o = {success: main_function_step2, fail: main_function_a_step1_fail};
  a(o);
}

function main_function_step2(data) {
  // execute code here that goes after function a, but before function b
  function main_function_a_step2_fail(err)
  {
    // handle error in step 2
  }
  var o = {success: main_function_step3, fail: main_function_a_step2_fail};
  b(o);
}

function main_function_step3(data) {
  // execute code here that goes after function b, but before function c
  function main_function_a_step3_fail(err)
  {
    // handle error in step 3
  }
  var o = {success: main_function_finish, fail: main_function_a_step3_fail};
  c(o);
}

function main_function_finish(data)
{
  // execute whatever code here to finish
}

代码有点混乱,但它不需要轮询或全局变量。如果步骤之间没有要执行的代码,并且它们之间的逻辑完全相同,则可以参数化数据结构中的所有内容,并使用单个函数执行所有步骤,该函数仅执行数据结构中的一项并传递下一项值作为回调等等。

According to the explanation for waitthread.js, it just uses a timer to poll, waiting for the change of the value of some variable.

The more typical design pattern for this type of problem uses callbacks and function a will call a callback when it's work is done which will kick off function b. So, you pass a function to function a which should be called when function a has finished it's work. The more complete design pattern usually has a callback for both a successful exit and an unsuccessful exit and may even pass parameters from the exit, but since I don't know your specifics, I haven't tried to model that here. For a multi-step process, it would look like this. We assume that three asynchronous functions exist as functions a, b and c and each takes a function as an argument that will get called when the asynchronous function is complete:

function main_function_step1() {
  a(main_function_step2);
}

function main_function_step2() {
  // execute code here that goes after function a, but before function b
  b(main_function_step3);
}

function main_function_step3() {
  // execute code here that goes after function b, but before function c
  c(main_function_finish);
}

function main_function_finish()
{
  // execute whatever code here to finish
}

The more complete solution would pass an object with a success callback and a failure callback and would provide at least a parameter to each function so results or error conditions can be returned.

In that case, it would look like this:

function main_function_step1() {
  function main_function_a_step1_fail(err)
  {
    // handle error in step 1
  }
  var o = {success: main_function_step2, fail: main_function_a_step1_fail};
  a(o);
}

function main_function_step2(data) {
  // execute code here that goes after function a, but before function b
  function main_function_a_step2_fail(err)
  {
    // handle error in step 2
  }
  var o = {success: main_function_step3, fail: main_function_a_step2_fail};
  b(o);
}

function main_function_step3(data) {
  // execute code here that goes after function b, but before function c
  function main_function_a_step3_fail(err)
  {
    // handle error in step 3
  }
  var o = {success: main_function_finish, fail: main_function_a_step3_fail};
  c(o);
}

function main_function_finish(data)
{
  // execute whatever code here to finish
}

This is a little messy to code, but it requires no polling or global variables. If there's no code to execute between the steps and the logic between them is all the same, you can parameterize everything in a data structure and execute all the steps with a single function that just executes one item out of the data structure and passes the next values as callbacks and so on.

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