我正在考虑使用 Web Worker 编写一个长时间运行的 CPU 密集型操作,似乎确实有可能将客户端的 CPU 使用率提高到 100%。 (参见 Can Web Workers utilize 100% of a multi-core CPU?)
有谁知道有什么有效的方法可以限制 web worker 的 CPU 使用率吗?
最佳答案
确实有办法。
构建您的计算,以便它们可以通过重复调用在退出前执行部分工作的函数来完成。在退出之前,使用 setTimeout 在几毫秒内安排对辅助函数的新调用。可以调整等待时间以使用更少或更少的 CPU 时间。
function doWork () {
var timer = new Date ();
// do n cyles of work here
timer = new Date () - timer; // time spent working
setTimeout (doWork, timer); // wait an equivalent time for 50% processor load
}
关于javascript - 限制 Web Worker CPU 利用率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12999891/