console
var worker;
function $(id){
return document.getElementById(id);
}
function startMyWorker(){
if(typeof(worker)!=="undefined")
{
var tenIntger=new Array();
function createTenIntger(){
for (var j=0;j<10;j++){
tenIntger[j]= Math.floor(Math.random()*90+10);
}
postMessage(tenIntger.sort());
setTimeout("createTenIntger",1000);
}
createTenIntger();
}
else{
$("result").innerHTML="对不起,您的浏览器不支持WebWorker..."
}
}
function stopMyWorker(){
worker.terminate();
}
<!DOCTYPE html>
<html>
<head>
<title>WebWorker应用</title>
</head>
<body>
<h3>随机产生10个100以内的2位整数:</h3>
<P> <output id="result"></output> </P>
<button onclick="startMyWorker()">开始 Worker-每秒产生10个整数</button><br>
<button onclick="stopMyWorker()">停止 Worker</button>
</body>
</html>