关于html5的webworker的简单示例

先不废话了,具体webworker是干什么用的自己拿baidu google去。以下仅是一个示例:

 1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>Worker</title>
5 <script src="resources/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
6 <script>
7 $(function () {
8 var worker1 = new Worker('resources/scripts/webworker1.js');
9 worker1.onmessage = function (event) {
10 $('#result').html("Wow, the data is" + event.data);
11 };
12
13 new Worker('resources/scripts/webworker2.js').onmessage = function () {
14 $('#Div1').html("Wow, the 2nd worker data is" + event.data);
15 }
16 });
17 </script>
18 </head>
19 <body>
20 <div>
21 <div id="result">
22 </div>
23 <div id="Div1">
24 </div>
25 </div>
26 </body>
27 </html>


webworker1.js:

var n = 1;
search: while (true) {
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1) {
if (n % i == 0) {
continue search;
}
}
// found a prime!
postMessage(n);
}

webworker2.js:

var test = 1;
search: while (true) {
test += 10;
for (var i = 2; i <= Math.sqrt(test); i += 1) {
if (test % i == 0) {
continue search;
}
}
// foutestd a prime!
postMessage(test);
}



posted @ 2011-12-08 14:37  猪在飞啊  阅读(834)  评论(0编辑  收藏  举报