Skip to content

Instantly share code, notes, and snippets.

@abhiyerra
Created July 10, 2009 05:41
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 abhiyerra/144259 to your computer and use it in GitHub Desktop.
Save abhiyerra/144259 to your computer and use it in GitHub Desktop.
/* fib.js */
i = 2;
while(true) {
postMessage(i + " " + fib(i));
i++;
}
function fib(n){
return n < 2 ? n : fib(n-1) +fib(n-2);
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Index</title>
<script type="text/javascript" charset="utf-8">
fib = new Worker('fib.js');
sayhello = new Worker('sayhello.js');
fib.onmessage = function(event) {
document.getElementById("fib").innerHTML = event.data;
}
sayhello.onmessage = function(event) {
document.getElementById("sayHello").innerHTML = event.data;
}
function callSayHello(f) {
document.getElementById("sayHelloNoThread").innerHTML = "NoThread:" + f.value;
sayhello.postMessage(f.value);
}
</script>
</head>
<body>
<h1>Fib</h1>
<span id="fib"></span>
<h1>Say Hello</h1>
<input type="text" onkeyup="callSayHello(this);" /><br/>
<span id="sayHello"></span><br/>
<span id="sayHelloNoThread"></span>
</body>
</html>
/* sayhello.js */
function sayHello(v) {
return "Hello there from thread " + v;
}
onmessage = function(event) {
postMessage(sayHello(event.data));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment