API | Description |
|---|---|
Creates a Worker thread in the main thread. | |
Enables the main thread and the Worker thread to send messages to each other. | |
Enables the main thread and the Worker thread to receive messages from each other. | |
Terminates the Worker thread in the main thread. |
Enables the main thread and the Worker thread to send messages to each other.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
message | object | Yes | Message content to be sent between the main thread and the Worker thread. |
// The main thread sends a message to the Worker thread.
// Here worker is the object returned by the qg.createWorker API called by the main thread.
worker.postMessage({
msg: 'hello from main'
})
// The Worker thread sends a message to the main thread.
// In the Worker thread, worker is a global variable.
worker.postMessage({
msg: 'hello from worker'
}) Enables the main thread and the Worker thread to receive messages from each other.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
listener | function | Yes | Message content received by the main thread and the Worker thread from each other. For details, please refer to listener. |
// The main thread receives a message sent by the Worker thread.
// Here worker is the object returned by the qg.createWorker API called by the main thread.
worker.onMessage((message) => {
console.log(`received message from worker: ${message}`)
})
// The Worker thread receives a message sent by the main thread.
// In the Worker thread, worker is a global variable.
worker.onMessage((message) => {
console.log(`received message from main: ${message}`)
})