Skip to content

Instantly share code, notes, and snippets.

@Zibri
Forked from SunboX/inlineworker.js
Created June 28, 2018 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zibri/451d34547b5b11b200f93f051acc4631 to your computer and use it in GitHub Desktop.
Save Zibri/451d34547b5b11b200f93f051acc4631 to your computer and use it in GitHub Desktop.
Create web workers without a separate worker JS files. Source: http://jsbin.com/owogib/8/
function worker() {
setInterval(function() {
postMessage({foo: "bar"});
}, 1000);
}
var code = worker.toString();
code = code.substring(code.indexOf("{")+1, code.lastIndexOf("}"));
var blob = new Blob([code], {type: "application/javascript"});
var worker = new Worker(URL.createObjectURL(blob));
worker.onmessage = function(m) {
console.log("msg", m);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment