本文介绍了在Javascript中执行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在客户端上运行cpu密集型任务。理想情况下,我希望能够使用jquery调用该函数并触发进度事件,以便我可以更新UI。

I have a cpu intensive task that I need to run on the client. Ideally, I'd like to be able to invoke the function and trigger progress events using jquery so I can update the UI.

我知道javascript不支持线程,但我看到一些有希望的文章尝试使用setTimeout来模仿线程。

I know javascript does not support threading, but I've seen a few promising articles trying to mimic threading using setTimeout.

最好的方法是什么?谢谢。

What is the best approach to use for this? Thanks.

推荐答案

我有一个类似的问题要解决,我需要保持我的UI线程免费,同时处理一些数据来显示。

I had a similar problem to solve recently where i needed to keep my UI thread free while crunching some data to display.

我写了一个库Background.js来处理几个场景:一个顺序的后台队列(基于WorkerQueue库),一个作业列表,每个作业调用每个计时器和数组迭代器,以帮助将您的工作分解成更小的块。示例和代码:

I wrote a library Background.js to handle a few scenarios: a sequential background queue (based on the WorkerQueue library), a list of jobs where each is called on every timer, and an array iterator to help break up your work into smaller chunks. Examples and code here: https://github.com/kmalakoff/background

享受!

这篇关于在Javascript中执行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:58
查看更多