Created
October 1, 2010 23:24
-
-
Save rjack/607058 to your computer and use it in GitHub Desktop.
Unstoppable Web Worker
This file contains 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
<!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> |
This file contains 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
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