御品VPS

御品VPS---独特的心情尽在御品VPS

HTML5 Web Workers

<div> <div> <h1>HTML5 <span>Web Workers</span> </h1>


<p>web worker 是运行在后台的 JavaScript,不会影响页面的性能。</p>
<h2>什么是 Web Worker?</h2> <p>当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成。</p> <p>web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能。您可以继续做任何愿意做的事情:点击、选取内容等等,而此时 web worker 在后台运行。</p>
<h2>浏览器支持</h2> <p> HTML5 Web WorkersHTML5 Web WorkersHTML5 Web WorkersHTML5 Web WorkersHTML5 Web Workers</p> <p>Internet Explorer 10, Firefox, Chrome, Safari 和 Opera 都支持Web workers.</p>
<h2>HTML5 Web Workers 实例</h2> <p>下面的例子创建了一个简单的 web worker,在后台计数:</p> <div> <h2>实例</h2> <div> 计数: <output></output><p> <button onclick="startWorker()">开始 Worker</button> <button onclick="stopWorker()">停止 Worker</button>

<script> var w; function startWorker() { if(typeof(Worker)!=="undefined") { if(typeof(w)=="undefined") { w=new Worker("/try/demo_source/demo_workers.js"); } w.onmessage = function (event) { document.getElementById("result").innerHTML=event.data; }; } else { document.getElementById("result").innerHTML="抱歉,你的浏览器不支持 Web Workers..."; } } function stopWorker() { w.terminate(); w = undefined; } </script> </p> </div>
</div> <p>demo_workers.js 文件代码:</p> <div> <div> var i=0;

function timedCount()
{
    i=i+1;
    postMessage(i);
    setTimeout("timedCount()",500);
}

timedCount(); </div></div>

<h2>检测浏览器是否支持 Web Worker</h2> <p>在创建 web worker 之前,请检测用户的浏览器是否支持它:</p> <div><div> if(typeof(Worker)!=="undefined")
{
    // 是的! Web worker 支持!
    // 一些代码.....
}
else
{
    //抱歉! Web Worker 不支持
} </div></div>

<h2>创建 web worker 文件</h2> <p>现在,让我们在一个外部 JavaScript 中创建我们的 web worker。</p> <p>在这里,我们创建了计数脚本。该脚本存储于 "demo_workers.js" 文件中:</p> <div> <div> var i=0;

function timedCount()
{
    i=i+1;
    postMessage(i);
    setTimeout("timedCount()",500);
}

timedCount(); </div> </div> <p>以上代码中重要的部分是 postMessage() 方法 - 它用于向 HTML 页面传回一段消息。</p> <p>注意: web worker 通常不用于如此简单的脚本,而是用于更耗费 CPU 资源的任务。</p>
<h2>创建 Web Worker 对象</h2> <p>我们已经有了 web worker 文件,现在我们需要从 HTML 页面调用它。</p> <p>下面的代码检测是否存在 worker,如果不存在,- 它会创建一个新的 web worker 对象,然后运行 "demo_workers.js" 中的代码:</p> <div><div> if(typeof(w)=="undefined")
{
    w=new Worker("demo_workers.js");
}</div></div> <p>然后我们就可以从 web worker 发生和接收消息了。</p> <p>向 web worker 添加一个 "onmessage" 事件监听器:</p> <div><div> w.onmessage=function(event){
    document.getElementById("result").innerHTML=event.data;
}; </div></div> <p当 web worker 传递消息时,会执行事件监听器中的代码。event.data 中存有来自 event.data 的数据。</p>
<h2>终止 Web Worker</h2> <p>当我们创建 web worker 对象后,它会继续监听消息(即使在外部脚本完成之后)直到其被终止为止。</p> <p>如需终止 web worker,并释放浏览器/计算机资源,请使用 terminate() 方法:</p> <div><div> w.terminate(); </div></div>

<h2>完整的 Web Worker 实例代码</h2> <p>我们已经看到了 .js 文件中的 Worker 代码。下面是 HTML 页面的代码:</p> <div> <h2>实例</h2> <div> <div><span><</span><span>!</span><span>DOCTYPE</span><span> </span><span>html</span><span>></span><span> </span><span><</span><span>html</span><span>></span><span> </span><span><</span><span>head</span><span>></span><span>  </span><span><</span><span>meta</span><span> </span><span>charset</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>></span><span>  </span><span><</span><span>title</span><span>></span><span>菜鸟教程(runoob.com)</span><span></</span><span>title</span><span>></span><span>  </span><span></</span><span>head</span><span>></span><span> </span><span><</span><span>body</span><span>></span><span> </span><span><</span><span>p</span><span>></span><span>计数: </span><span><</span><span>output</span><span> </span><span>id</span><span>=</span><span>"</span><span>result</span><span>"</span><span>></span><span></</span><span>output</span><span>></span><span></</span><span>p</span><span>></span><span> </span><span><</span><span>button</span><span> </span><span>onclick</span><span>=</span><span>"</span><span>startWorker()</span><span>"</span><span>></span><span>开始工作</span><span></</span><span>button</span><span>></span><span> </span><span><</span><span>button</span><span> </span><span>onclick</span><span>=</span><span>"</span><span>stopWorker()</span><span>"</span><span>></span><span>停止工作</span><span></</span><span>button</span><span>></span><span> </span><span><</span><span>p</span><span>></span><span><</span><span>strong</span><span>></span><span>注意:</span><span></</span><span>strong</span><span>></span><span> Internet Explorer 9 及更早 IE 版本浏览器不支持 Web Workers.</span><span></</span><span>p</span><span>></span><span> </span><span><</span><span>script</span><span>></span></div> <div><span>var</span><span> </span><span>w</span><span>; </span><span>function</span><span> </span><span>startWorker</span><span>(</span><span>)</span><span> </span><span>{</span><span> </span><span>if</span><span>(</span><span>typeof</span><span>(</span><span>Worker</span><span>)</span><span> !== </span><span>"</span><span>undefined</span><span>"</span><span>)</span><span> </span><span>{</span><span> </span><span>if</span><span>(</span><span>typeof</span><span>(</span><span>w</span><span>)</span><span> == </span><span>"</span><span>undefined</span><span>"</span><span>)</span><span> </span><span>{</span><span> </span><span>w</span><span> = </span><span>new</span><span> </span><span>Worker</span><span>(</span><span>"</span><span>demo_workers.js</span><span>"</span><span>)</span><span>; </span><span>}</span><span> </span><span>w</span><span>.</span><span>onmessage</span><span> = </span><span>function</span><span>(</span><span>event</span><span>)</span><span> </span><span>{</span><span> </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>result</span><span>"</span><span>)</span><span>.</span><span>innerHTML</span><span> = </span><span>event</span><span>.</span><span>data</span><span>; </span><span>}</span><span>; </span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span> </span><span>document</span><span>.</span><span>getElementById</span><span>(</span><span>"</span><span>result</span><span>"</span><span>)</span><span>.</span><span>innerHTML</span><span> = </span><span>"</span><span>抱歉,你的浏览器不支持 Web Workers...</span><span>"</span><span>; </span><span>}</span><span> </span><span>}</span><span> </span><span>function</span><span> </span><span>stopWorker</span><span>(</span><span>)</span><span> </span><span>{</span><span> </span><span>w</span><span>.</span><span>terminate</span><span>(</span><span>)</span><span>; </span><span>w</span><span> = </span><span>undefined</span><span>; </span><span>}</span></div> <div><span></</span><span>script</span><span>></span><span> </span><span></</span><span>body</span><span>></span><span> </span><span></</span><span>html</span><span>></span></div> </div>
</div>

<h2>Web Workers 和 DOM</h2> <p>由于 web worker 位于外部文件中,它们无法访问下列 JavaScript 对象:</p> <ul> <li>window 对象</li> <li>document 对象</li> <li>parent 对象</li> </ul> </div> </div>

未经允许不得转载:御品VPS » HTML5 Web Workers

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址