使用外部等待类暂停 javascript 函数的执行
我试图弄清楚如何在调用另一个函数之前暂停函数的执行,同时使我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据waitthread.js的解释,它只是使用一个定时器来轮询,等待某个变量值的变化。
此类问题的更典型设计模式使用回调,并且
函数 a
将在工作完成时调用回调,从而启动函数 b
。因此,您将一个函数传递给function a
,当function a
完成其工作时应该调用该函数。更完整的设计模式通常有成功退出和不成功退出的回调,甚至可能从退出传递参数,但由于我不知道你的具体情况,所以我没有尝试在这里建模。对于多步骤过程,它看起来像这样。我们假设三个异步函数作为函数 a、b 和 c 存在,并且每个函数都采用一个函数作为参数,该函数将在异步函数完成时被调用:更完整的解决方案将传递一个带有成功回调和失败回调的对象,将为每个函数至少提供一个参数,以便可以返回结果或错误条件。
在这种情况下,它看起来像这样:
代码有点混乱,但它不需要轮询或全局变量。如果步骤之间没有要执行的代码,并且它们之间的逻辑完全相同,则可以参数化数据结构中的所有内容,并使用单个函数执行所有步骤,该函数仅执行数据结构中的一项并传递下一项值作为回调等等。
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 offfunction b
. So, you pass a function tofunction a
which should be called whenfunction 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: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:
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.