function createWorker() {
const script_content = `self.addEventListener('message', function(e) {
const result = e.data[0] + e.data[1];
postMessage(result); // 将结果发送回主线程
});`;
const blob2 = new Blob([script_content], { type: "text/javascript" });
const myWorker = new Worker(URL.createObjectURL(blob2), { type: 'module' });
return myWorker;
}
let myWorker = createWorker();
myWorker.addEventListener('message', function (e) {
console.log('Result from worker: ' + e.data);
});
myWorker.postMessage([1, 2]);