Execute Background Task In Javascript

后端 未结 9 1628
迷失自我
迷失自我 2020-11-28 22:39

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

9条回答
  •  悲哀的现实
    2020-11-28 23:35

    Great answer Kevin! I wrote something similar a few years back, though less sophisticated. Source code is here if anyone wants it:

    http://www.leapbeyond.com/ric/jsUtils/TaskQueue.js

    Anything with a run() method can be queued as a task. Tasks can re-queue themselves to perform work in chunks. You can prioritize tasks, add/remove them at will, pause / resume the entire queue, etc. Works well with asynchronous operations - my original use for this was to manage several concurrent XMLHttpRequests.

    Basic usage is pretty simple:

    var taskQueue = new TaskQueue();
    taskQueue.schedule("alert('hello there')");
    

    The header comments in the .js file provide more advanced examples.

提交回复
热议问题