Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Background Task In Javascript

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.

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.

like image 848
Rob Avatar asked Jul 21 '09 15:07

Rob


People also ask

How API works in background?

The Cooperative Scheduling of Background Tasks API (also referred to as the Background Tasks API or the requestIdleCallback() API) provides the ability to queue tasks to be executed automatically by the user agent when it determines that there is free time to do so.

What is background task scheduler?

A class for scheduling task requests that launch your app in the background.

How do I run background service continuously in react native?

Currently, there is, unfortunately, no support for background tasks of any kind. The feature you are referring to would be a background timer. Such a timer is this product pain (a feature request) for react native, you may upvote it to show an increased demand for this feature.

What is Android background task?

Definition of background work. An app is running in the background when both the following conditions are satisfied: None of the app's activities are currently visible to the user. The app isn't running any foreground services that started while an activity from the app was visible to the user.


2 Answers

Basically, what you want to do is to divide the operation into pieces. So say you have 10 000 items you want to process, store them in a list and then process a small number of them with a small delay between each call. Here's a simple structure you could use:

function performTask(items, numToProcess, processItem) {     var pos = 0;     // This is run once for every numToProcess items.     function iteration() {         // Calculate last position.         var j = Math.min(pos + numToProcess, items.length);         // Start at current position and loop to last position.         for (var i = pos; i < j; i++) {             processItem(items, i);         }         // Increment current position.         pos += numToProcess;         // Only continue if there are more items to process.         if (pos < items.length)             setTimeout(iteration, 10); // Wait 10 ms to let the UI update.     }     iteration(); }  performTask(     // A set of items.     ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'],     // Process two items every iteration.     2,     // Function that will do stuff to the items. Called once for every item. Gets     // the array with items and the index of the current item (to prevent copying     // values around which is unnecessary.)     function (items, index) {         // Do stuff with items[index]         // This could also be inline in iteration for better performance.     }); 

Also note that Google Gears has support to do work on a separate thread. Firefox 3.5 also introduced its own workers that do the same thing (although they follow the W3 standard, while Google Gears uses its own methods.)

like image 174
Blixt Avatar answered Oct 06 '22 12:10

Blixt


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

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

Enjoy!

like image 38
Kevin Avatar answered Oct 06 '22 12:10

Kevin