wx.CreateWorker
wx.createWorker(string scriptPath)
Function: Create a worker thread.
Parameters: string scriptPath, the absolute path of the worker entry file.
Return:Worker,Worker object.
Notes:
Before using the interface, you need to configure the workers field in app.json, it means the root directory of the worker code;
scriptPath is the absolute path of the entry file, and does not start with /;
Currently, only one worker can be created at most, please calll Worker.terminate before creating the next worker. Sample code
const worker = wx.createWorker('workers/index.js')
worker.onMessage(function (res) {
console.log(res)
})
worker.postMessage({
msg: 'hello worker'
})
worker.terminate()
Worker
Worker.onMessage
Worker.onMessage(function listener)
Feature Description:Listen to the event of the message sent by the main thread/worker thread to the current thread.
Parameter 1:function listener, the event listener for the message sent by the main thread/worker thread to the current thread.
Parameter 2:Object res
|
message | Object | Message sent from the main/Worker thread to the current thread |
Worker.postMessage
Worker.postMessage(Object message)
Feature Description:Message sent to the main/Worker thread.
Parameters:Object message, the message to be sent.
Sample code:
worker thread
worker.postMessage({
msg: 'hello from worker'
})
main thread
const worker = wx.createWorker('workers/request/index.js')
worker.postMessage({
msg: 'hello from main'
})
Woker.terminate
Worker.terminate()
Feature Description:Ends the current Worker thread. Call only on the worker object of the main thread.
Was this page helpful?