tencent cloud

Feedback

Last updated: 2024-11-21 18:41:53

    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
    // Create a regular worker
    const worker = wx.createWorker('workers/index.js') // The filename specifies the path to the worker's entry file.
    
    
    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
    Attributes
    Types
    Description
    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.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support