This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myWorker = new Worker("src/worker.js"); // A | |
myWorker.postMessage('Message from index.html'); // B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = (e) => { | |
console.log(e.data); | |
const { name } = e.data; | |
postMessage({ | |
type: "success", | |
message: `Hello ${name} from worker.js` | |
}); | |
close(); //<-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof Worker === "undefined") { | |
document.getElementById("app").innerHTML = "Sorry, your browser does not support Web Workers..."; | |
} else { | |
//... | |
myWorker.addEventListener('message', e => { | |
document.getElementById("app").innerHTML = e.data; // A | |
console.log("=> ", e.data); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = (e) => { | |
timedCount(); // D | |
}; | |
// A | |
function timedCount() { | |
let i = 0; | |
setInterval(() => { | |
i++; | |
postMessage(i); // B |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof Worker === "undefined") { | |
document.getElementById("app").innerHTML = "Sorry, your browser does not support Web Workers..."; | |
} else { | |
const myWorker = new Worker("src/worker.js"); | |
myWorker.postMessage(""); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = (e) => { | |
console.log(e.data); | |
//postMessage(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof Worker === "undefined") { | |
//... | |
} else { | |
//... | |
myWorker.addEventListener('message', e => { | |
console.log(e.data); | |
myWorker.terminate(); // <- | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof Worker === "undefined") { | |
//... | |
} else { | |
//... | |
myWorker.addEventListener('message', e => { | |
console.log(e.data); | |
myWorker.terminate(); // <- | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = (e) => { | |
console.log(e.data); | |
const { name } = e.data; | |
postMessage({ | |
type: 'success', | |
message: `Hello ${name} from worker.js` | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof Worker === "undefined") { | |
//... | |
} else { | |
//... | |
// A | |
myWorker.addEventListener('message', e => { | |
console.log(e.data); // B | |
}); | |
} |
NewerOlder