Skip to content

Instantly share code, notes, and snippets.

@WolfieWerewolf
Forked from akirattii/webworker.example.js
Created February 24, 2018 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WolfieWerewolf/204a99c6c620b4242f28caf92c603f34 to your computer and use it in GitHub Desktop.
Save WolfieWerewolf/204a99c6c620b4242f28caf92c603f34 to your computer and use it in GitHub Desktop.
WebWorker example using importScript
Webworker
// worker.js
var ju;
addEventListener('message', function(e) {
if (e.data == "dummy") {
importScripts('./JapaneseUtil.js');
console.log("this worker on message...", e);
if (!ju) ju = JapaneseUtil();
let arr = ju.separate("私は、日本語が得意です。");
postMessage(arr);
}
}, false);
// main.js
var worker;
document.querySelector("#btn3").onclick = function() {
if(!worker) {
worker = new Worker("worker.js");
worker.onmessage = function (e) {
console.log("e", e);
}
}
worker.postMessage("dummy"); // Start the worker.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment