动态创建JS WORKER

JavaScript 2021年7月14日 26 热度
window.URL = window.URL || window.webkitURL;
const response = `self.onmessage = ({ data: { data } }) => {
  const {method} = data;
  self.postMessage(method.replace("吗",""));
}`;
const blob = new Blob([response],{ type: "application/javascript" });
const work = new Worker(URL.createObjectURL(blob));
work.onmessage = function (e) {
 //  speechSynthesis.speak(new SpeechSynthesisUtterance(e.data))
  alert(`Response: ${JSON.stringify(e)}`);
//   URL.revokeObjectURL(url);//释放内存
//   worker.terminate();
};
worker.postMessage({
  method: "吃饭了吗"
});

动态创建 Web Worker 实践指南

声明:Shire.随记 | 随记,关于前端、产品、分享|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 动态创建JS WORKER