As far as I can tell, web workers need to be written in a separate JavaScript file, and called like this:
new Worker('longrunning.js')
I'm using the closure compiler to combine and minify all my JavaScript source code, and I'd rather not have to have my workers in separate files for distribution. Is there some way to do this?
new Worker(function() {
//Long-running work here
});
Given that first-class functions are so crucial to JavaScript, why does the standard way to do background work have to load a whole 'nother JavaScript file from the server?
You can use web workers in same javascript fie using inline webworkers.
The below article will address you to easily understand the webworkers and their limitations and debugging of webworkers.
Mastering in webworkers
here console:
I discovered that CodePen currently does not syntax-highlight inline
<script>
tags that are nottype="text/javascript"
(or which have no type attribute).So I devised a similar but slightly different solution using labeled blocks with
break
, which is the only way you can bail from a<script>
tag without creating a wrapper function (which is unnecessary).Depending on your use case you can use something like
A example would be
http://www.html5rocks.com/en/tutorials/workers/basics/#toc-inlineworkers
Full example of BLOB inline worker:
Try to use jThread. https://github.com/cheprasov/jThread