简体   繁体   English

javascript中的异步编程(非AJAX)

[英]Asynchronous programming in javascript (NOT AJAX)

Is it possibly to do things asynchronously in javascript (AJAX aside)? 是不是可以在javascript(AJAX旁边)中异步处理? For example, to iterate multiple arrays at the same time. 例如,要同时迭代多个数组。 How is it done? 怎么做? A brief example would be nice. 一个简短的例子会很好。 Searching for this was hard, due to all the ajax pollution, which is not what I am looking for. 由于所有的ajax污染,寻找这个很难,这不是我想要的。

Thanks in advance. 提前致谢。

Use web Workers . 使用web Workers But remember that it is a very new feature and not all browsers are fully supported. 但请记住,这是一项非常新的功能,并非所有浏览器都得到完全支持。

You could use setTimeout . 你可以使用setTimeout

setTimeout(function () { iterateArray(array1); reportDone(1); }, 0);
setTimeout(function () { iterateArray(array2); reportDone(2); }, 0);

I'm not sure how concurrent it will be, but it is an asynchronous programming model. 我不确定它会是多么并发,但它是一个异步编程模型。

As stated by Grumdrig you can write code like this: 正如Grumdrig所说,您可以编写如下代码:

setTimeout(function () { iterateArray(array1); reportDone(1); }, 0);
setTimeout(function () { iterateArray(array2); reportDone(2); }, 0);

But it will still not run concurrently. 但它仍然不会同时运行。 Here's a general idea of what happens after such timeouts are called: 以下是调用此类超时后发生的事情的一般概念:

  • Any code after the setTimeout calls will be run immediately, including returns to calling functions. setTimeout调用之后的任何代码都将立即运行,包括返回调用函数。
  • If there are other timers in queue that are at or past their delay or interval time, they will be executed one at a time. 如果队列中有其他定时器处于或超过其延迟或间隔时间,则它们将一次执行一个。
  • While any timer is running, another might hit its interval/delay time, but it will not be run until the last one is finished. 当任何计时器正在运行时,另一个计时器可能会达到其间隔/延迟时间,但在最后一个计时器完成之前不会运行。
  • Some browsers give priority to events fired from user interaction such as onclick and onmousemove , in which case the functions attached to those events will execute at the expense of timer accuracy. 某些浏览器优先考虑从用户交互中触发的事件,例如onclickonmousemove ,在这种情况下,附加到这些事件的函数将以定时器精度为代价执行。
  • This will continue until there is an opening (no previously called timers or event handlers requesting execution). 这将持续到有一个开口(之前没有被称为定时器或事件处理程序请求执行)。 Only then will the functions in the example code be run. 只有这样才能运行示例代码中的函数。 Again one at a time, with the first one likely but not certainly executing first. 一次一个,第一个可能但不一定先执行。 Also, I'm venturing a guess that some browsers might impose a minimum delay time, which would make any timers set with a delay of 0 milliseconds be run even later than expected. 此外,我猜测一些浏览器可能会施加最小延迟时间,这将使任何设置延迟为0毫秒的定时器运行甚至比预期更晚。

Obviously there is no performance advantage to running code like this. 显然,运行这样的代码没有性能优势。 In every case it will make things take longer to complete. 在每种情况下,它都会使事情需要更长时间才能完成。 However in cases where a single task is taking so long it freezes the browser (and possibly trips "Script is taking too long" browser warnings), it can be helpful to break it up into smaller faster executing pieces that run sequentially after some delay time, thus giving the browser some time to breathe. 但是,如果单个任务花了这么长时间,它会冻结浏览器(并且可能会导致“脚本花费太长时间”浏览器警告),将它分解成更小的更快的执行部分会有所帮助,这些部分会在一段延迟时间后按顺序运行,从而给浏览器一些时间来呼吸。

Web Workers have been mentioned, and if you are not concerned about IE compatibility then you can use them for true concurrency. Web Workers已被提及,如果您不关心IE兼容性,那么您可以使用它们来实现真正的并发性。 However there are some severe limitations on their use imposed for security reasons. 然而,出于安全原因,对其使用存在严重限制。 For one they cannot interact with the DOM in any way, meaning any changes to the page still must be done synchronously. 对于其中一个,他们无法以任何方式与DOM交互,这意味着对页面的任何更改仍然必须同步完成。 Also all data passed to and from workers is serialized in transit, meaning true Javascript objects cannot be used. 此外,传递给工作人员和从工作人员传递的所有数据都在传输中序列化,这意味着无法使用真正的Javascript对象。 That being said, for intensive data processing, Web Workers are probably a better solution than breaking a function up into multiple timer delayed tasks. 话虽如此,对于密集型数据处理,Web Workers可能是比将功能分解为多个定时器延迟任务更好的解决方案。

该领域的一个新发展是HTML5 Web Workers。

JavaScript is normally single threaded; JavaScript通常是单线程的; you cannot do several things at once. 你不能一次做几件事。 If your JavaScript code is too slow, you will need to offload the work. 如果您的JavaScript代码太慢,则需要卸载工作。 The new way is to use web workers , as others have noted. 其他人已经注意到,新方法是使用网络工作者 The old way is often to use AJAX and do the work on the server instead. 旧的方法通常是使用AJAX,而是在服务器上完成工作。 (Either with web workers or with AJAX, the arrays would have to be serialized and the result deserialized) (无论是使用Web worker还是使用AJAX,都必须序列化数组并对结果进行反序列化)

I have to agree with MooGoo, i also wonder why you would run through such a big array in one go. 我不得不同意MooGoo,我也想知道你为什么要一次性经历如此庞大的阵列。

There's an extension to JavaScript called StratifiedJS , it allows you do multiple things at once as long as they're asynchronous. 有一个名为StratifiedJS的 JavaScript扩展,只要它们是异步的,它允许你同时做多个事情。 Also, webworkers are an awkward "solution" that just make things more complicated, also, they don't work in IE. 此外,网络工作者是一个尴尬的“解决方案”,只是让事情变得更复杂,而且,它们在IE中不起作用。

In StratifiedJS you could just write. 在StratifiedJS你可以写。

waitfor {
   // do something long lasting here...
}
and {
  // do something else at the same time...
}
// and when you get here, both are done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM