Menu

gitpiper

Web workers Cheat Sheet in February 2023

Last Updated: 1 March 2023

README.md

Web workers

Client

var worker = new Worker('worker.js')

worker.onmessage = function (message) {
  alert(JSON.stringify(message.data))
})

worker.postMessage('hello!')

Messages can be anything that can be serialized into JSON (objects, arrays, strings, numbers, booleans). See: structured clone

Worker

self.onmessage = function (message) {
  ···
}

self.postMessage({ msg: 'hello' })

Message data

[MessageEvent]

bubbles: false
cancelBubble: false
cancelable: false
clipboardData: undefined
currentTarget: Worker
data: "Hello"             ← the data
defaultPrevented: false
eventPhase: 0
lastEventId: ""
origin: ""
ports: Array[0]
returnValue: true
source: null
srcElement: Worker
target: Worker
timeStamp: 1344821022383
type: "message"

These are the contents of message on onmessage.


338+ more cheat sheets for you in February 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️