title: Taro.createWorker(scriptPath)

sidebar_label: createWorker

创建一个 Worker 线程。目前限制最多只能创建一个 Worker,创建下一个 Worker 前请先调用 Worker.terminate

参考文档

类型

  1. (scriptPath: string) => Worker

参数

参数 类型 说明
scriptPath string worker 入口文件的绝对路径

示例代码

  1. const worker = Taro.createWorker('workers/request/index.js') // 文件名指定 worker 的入口文件路径,绝对路径
  2. worker.onMessage(function (res) {
  3. console.log(res)
  4. })
  5. worker.postMessage({
  6. msg: 'hello worker'
  7. })
  8. worker.terminate()

API 支持度

API 微信小程序 H5 React Native
Taro.createWorker ✔️