May 19, 2021 WeChat Mini Program Development Document
Base library 1.9.90 starts to support, and low versions need to be compatible
Before using createWorker, consult the multithreaded documentation for the basics and configuration methods.
Create a Worker thread and return a Worker instance, currently limited to a maximum of one Worker, call Worker.terminate before you create the next Worker.
ScriptPath is the worker's entry file path and requires an absolute path.
A list of methods for worker objects:
Method | Parameters | Description |
---|---|---|
postMessage | Object | The message sent to the Worker thread. |
onMessage | callback | Listens for messages sent by the Worker thread to the current thread |
terminate | Ends the current Worker thread and is called only on the main thread, the Worker instance. |
PostMessage (message) Description:
Send a message to the Worker thread, the message parameter is the message that needs to be sent, and must be a serialized JavaScript object.
OnMessage (callback) callback results note:
Property | Type | Description |
---|---|---|
message | Object | The message sent by the Worker thread to the current thread |
The description of the terminate() is:
Ends the current worker thread and is called only on the main thread worker object.
Example code:
The following code needs to be configured first, see the multithreaded documentation for basic knowledge and configuration methods.
const worker = wx.createWorker('workers/request/index.js') // 文件名指定 worker 的入口文件路径,绝对路径
worker.onMessage(function (res) {
console.log(res)
})
worker.postMessage({
msg: 'hello worker'
})
worker.terminate()