worker 父執行緒與子執行緒並不會交叉執行
let child = new Worker("./child_1.js");
let it;
child.onmessage = function(e) {
let data = e.data;
console.log(`child ${data.index}`);
// 確保 parent loop 與 child 幾乎同時執行
it.next();
};
function* parentLoop(){
for (var i = 0; i < 100; i++) {
console.log(`parent ${i}`);
}
console.dir("parent finish");
}
function click() {
it = parentLoop();
child.postMessage({});
}
-----------------------------------------------------------------
onmessage = function(oEvent) {
for (var i = 0; i < 100; i++) {
postMessage({index:i});
}
};