MENU

Web Worker

The helper functions startworker (defines fromworker() as event-handler function), toworker, & stopworker are defined in theme/functions.js

The worker runs in an external js file: in this case "worker.js". Working example: start; stop; send message to; receive message from. "Start" creates the worker, "Stop" terminates it.

Workers are spawned and owned by a page. Workers are isolated so can't access the DOM nor Session variables. They can only receive and send messages (values or arrays) using the postMessage API. postMessage is asynchronous so doesn't return a message, but a "promise" to a message. Supported by every browser from IE10 onwards.

Good for computationally expensive things as they don't tie up the interface.

0

For a game 16.6ms of main-thread time is the frame-time your browser needs for 60fps. A worker can free up more of that time if implemented properly.

Start Zero Stop ms per frame