先不废话了,具体webworker是干什么用的自己拿baidu google去。以下仅是一个示例:
<!DOCTYPE HTML>
<html>
<head>
<title>Worker</title>
<script src="resources/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
$(function () {
var worker1 = new Worker('resources/scripts/webworker1.js');
worker1.onmessage = function (event) {
$('#result').html("Wow, the data is" + event.data);
};
new Worker('resources/scripts/webworker2.js').onmessage = function () {
$('#Div1').html("Wow, the 2nd worker data is" + event.data);
}
});
</script>
</head>
<body>
<div>
<div id="result">
</div>
<div id="Div1">
</div>
</div>
</body>
</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);
}