简体   繁体   English

JavaScript中的一个函数调用期间的多个更新

[英]Multiple updates during one function call in JavaScript

I'm trying to write some script performing quite heavy computations. 我正在尝试编写一些脚本执行相当繁重的计算。 Thus, I'd like to inform user about progress by updating content on the site (for example, fraction of completed computations). 因此,我想通过更新网站上的内容(例如,完成计算的一小部分)来通知用户进度。 Is it possible to achieve with JavaScript? 是否可以用JavaScript实现? Does JavaScript allow update of the content while invoked function is still running? JavaScript是否允许在调用函数仍在运行时更新内容? Or maybe no matter what are the circumstances I can update the content only after invoked function is done? 或者也许无论在什么情况下我都可以在调用函数完成后更新内容?

Take a look at workers: http://www.html5rocks.com/en/tutorials/workers/basics/ 看一看工人: http//www.html5rocks.com/en/tutorials/workers/basics/

It's made for heavy computation (at javascript scale ofc), and comes with some functions for updating the user about the state of the computation, it's multithreading in javascript basically. 它用于繁重的计算(在javascript规模的c),并附带一些函数来更新用户有关计算的状态,它基本上是在JavaScript中的多线程。

Use timers 使用计时器

From your computing function, update global variables. 从您的计算功能,更新全局变量。

Then from main thread, use setTimeout() or setInterval() to query the global value periodically and update screen. 然后从主线程中,使用setTimeout()setInterval()定期查询全局值并更新屏幕。

While your function is running, no screen updates will happen: UI thread has to fight for its time just like any other JS thread. 当您的函数正在运行时,不会发生任何屏幕更新:UI线程必须像其他任何JS线程一样争取它的时间。 You can do it by scheduling callbacks: 你可以通过安排回调来做到这一点:

function f() {
  a();
  // update now
  b();
}

can be rewritten as 可以改写为

function f() {
  a();
  setTimeout(function() {
    b();
  }, 0);
}

The way this works, after a() , setTimeout will schedule the rest of the function, then exit. 这种方式有效,在a()setTimeout将调度函数的其余部分,然后退出。 This is where UI thread can (and will) now grab control; 这是UI线程现在可以(并且将会)获取控制权的地方; then after it is done, the scheduled remainder will execute. 完成后,计划的剩余部分将执行。

If you have things to do that fit the (quite severe) WebWorker restrictions, you can use them instead; 如果您有适合(非常严格的)WebWorker限制的事情,您可以使用它们; however, they cannot touch UI, so redraws would be pointless (unless you do as Antoine says, and schedule an update function based on WebWorker-calculated parameters). 然而,他们无法触摸UI,因此重绘是没有意义的(除非你像Antoine所说,并根据WebWorker计算的参数安排更新功能)。

My few cents: 我的几分钱:

  • The easiest method to address is the most common way of simply showing a busy animation. 最简单的解决方法是简单显示繁忙动画的最常用方法。

  • Try breaking your heavyweight computational code into pieces & update the progress bar. 尝试将重量级计算代码分解成碎片并更新进度条。

  • Or as Antoine suggested, try using Web Workers . 或者如Antoine建议的那样,尝试使用Web Workers

Yes, probably you can do it. 是的,可能你可以做到。 I don't know all details of your problem, but you should be aimed to socket.io and asynchronous node.js . 我不知道你的问题的所有细节,但你应该针对socket.io和异步node.js。

With node.js you can send process states to clients in real time and catch server responses with socket.io on client-side. 使用node.js,您可以实时向客户端发送进程状态,并在客户端使用socket.io捕获服务器响应。

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

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