Skip to content

Instantly share code, notes, and snippets.

@rjack
Created October 1, 2010 23:24
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 rjack/607058 to your computer and use it in GitHub Desktop.
Save rjack/607058 to your computer and use it in GitHub Desktop.
Unstoppable Web Worker
<!DOCTYPE>
<html>
<head></head>
<body>
<p>Start the worker from the browser console (fire up a CPU monitor to
have some sort of visual feedback)</p>
<pre>worker.postMessage("run")</pre>
<p>you will NOT be able to stop it with</p>
<pre>worker.postMessage("stop")</pre>
<p>What I'd expected:</p>
<pre>worker.postMessage("run") // start
worker.postMessage("stop") // stop
worker.postMessage("run") // start from where it was interrupted</pre>
<script type="text/javascript">
var worker = new Worker("worker.js");
</script>
</body>
</html>
var count = 0,
running = false,
iface = {};
iface.run = function ()
{
running = true;
while (running) {
count++;
}
};
iface.stop = function ()
{
running = false;
};
onmessage = function (ev)
{
var func_name = ev.data;
iface[func_name].apply();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment