Javascript与VBA的DoEvents有什么相似之处吗? [英] Does Javascript have anything similar to VBA's DoEvents?

查看:229
本文介绍了Javascript与VBA的DoEvents有什么相似之处吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个长时间运行的for循环,我想延迟循环来处理事件队列中的其他任务(比如按下按钮)。 javascript或JQuery有什么可以帮助我吗?基本上我正在尝试做类似延迟循环的事情( https:// support。 microsoft.com/en-us/kb/118468 )。

I have a long running for-loop in my code and I'd like to delay to loop to handle other tasks in the event queue (like a button press). Does javascript or JQuery have anything that could help me? Basically I'm trying to do something similar to delaying loops like here (https://support.microsoft.com/en-us/kb/118468).

推荐答案

如果您的应用确实需要长期运行的JavaScript代码,处理它的最佳方法之一是使用 JavaScript网络工作者。 JavaScript代码通常在前台线程上运行,但是通过创建Web worker,您可以在后台线程上有效地保持长时间运行的进程,并且您的UI线程可以自由地响应用户输入。

If your application really requires long-running JavaScript code, one of the best ways to deal with it is by using JavaScript web workers. JavaScript code normally runs on the foreground thread, but by creating a web worker you can effectively keep a long-running process on a background thread, and your UI thread will be free to respond to user input.

例如,你创建一个这样的新工人:

As an example, you create a new worker like this:

var myWorker = new Worker("worker.js");

然后你可以从主页面的js发布消息,如下所示:

You can then post messages to it from the js in the main page like this:

myWorker.postMessage([first.value,second.value]);
console.log('Message posted to worker');

并回复 worker.js 像这样:

onmessage = function(e) {
  console.log('Message received from main script');
  var workerResult = 'Result: ' + (e.data[0] * e.data[1]);
  console.log('Posting message back to main script');
  postMessage(workerResult);
}

这篇关于Javascript与VBA的DoEvents有什么相似之处吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆