Web Workers
Web workers allow you to run JavaScript code in the background without blocking the main thread. This can be useful for performing long-running tasks, such as image processing, without slowing down the UI. Web workers communicate with the main thread using the postMessage and onmessage APIs.
Here's an example of how to create a web worker:
// worker.js
onmessage = function(event) {
console.log(event.data); // "Hello, World!"
postMessage('Hi there!');
};
// main.js
const worker = new Worker('worker.js');
worker.onmessage = function(event) {
console.log(event.data); // "Hi there!"
};
worker.postMessage('Hello, World!');
No comments:
Post a Comment