JavaScript

动态创建JS WORKER

2021-7-14 705字 578
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 发表于 2021-7-14
转载请注明: 动态创建JS WORKER | Shire

评论