HTML5之Worker用法
HTML5提供了Worker类用于多线程处理。Worker是在UI主线程中创建,后台执行的一段js脚本,它通过消息与UI线程传递数据。使用Worker就3步:
cheungmine 2011-11-29
第1步:创建一个Worker,需要指定一个js文件,作为Worker线程的执行体:
var worker = new Worker("worker.js");
第2步:给Worker实例指定消息处理函数,只有2个:onmessage ,onerror
worker.onmessage = function (event) {
// update UI here
var t1 = new Date().getTime();
elemById("_time").value = t1 - t0;
elemById("_piValue").value = event.data;
};
worker.onerror = function (event) {
alert(event.message);
};
第3步:给worker发消息:postMessage。
elemById("_time").value = "-";
t0 = new Date().getTime();
worker.postMessage(parseInt(elemById("_num_rects").value));
下面以一个具体的例子来说明Worker的用法。这个例子是用数值积分的方法求pi=(3.1415926....)的。worker.js就是做这个工作。
- // worker.js
- // calculate pi using numerical integration
- // 2011-11, cheungmine
- self.onmessage = function (event) {
- // numerical integration to calc pi
- var num_rects = event.data;
- var width = 1.0/num_rects;
- var mid;
- var height;
- var sum = 0.0;
- var area;
- for (var i=0; i<num_rects; i++) {
- mid = (i+0.5) * width;
- height = 4.0/(1.0+mid*mid);
- sum += height;
- }
- area = width*sum; // area=pi
- // post message back to UI thread
- self.postMessage(area);
- };
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>js-test.html</title>
- <script type="text/javascript">
- function elemById(id) {
- return document.getElementById(id);
- }
- function getBrowserAgent() {
- var browser = "$";
- if ((navigator.userAgent.indexOf('MSIE') >= 0) &&
- (navigator.userAgent.indexOf('Opera') < 0)){
- browser = "$IE";
- }
- else if (navigator.userAgent.indexOf('Firefox') >= 0){
- browser = "$FIREFOX";
- }
- else if (navigator.userAgent.indexOf('Opera') >= 0){
- browser = "$OPERA";
- }
- else if (navigator.userAgent.indexOf('Chrome') >= 0){
- browser = "$CHROME";
- }
- return browser;
- }
- var browser = getBrowserAgent();
- window.addEventListener('load',
- function () {
- if (browser=="$IE") {
- // MSIE
- if (window.confirm("IE does not support HTML5 currently.\n"+
- "please use lastest FireFox, Chrome or Opera!\n"+
- "if you havenot any of them installed,\n"+
- "please click OK to enter download page.")) {
- window.location.replace("selbrowser.html");
- }
- else {
- window.close();
- }
- }
- else if (browser=="$FIREFOX"||browser=="$CHROME") {
- // do stuff for FireFox and Chrome
- }
- else {
- // do stuff for others
- }
- initPage();
- },
- false
- );
- var dt = new Date();
- var t0 = 0;
- function initPage () {
- // onclick
- elemById("_piWorker").addEventListener('click',
- function(){
- var worker = new Worker("worker.js");
- worker.onmessage = function (event) {
- // update UI here
- var t1 = new Date().getTime();
- elemById("_time").value = t1 - t0;
- elemById("_piValue").value = event.data;
- };
- worker.onerror = function (event) {
- alert(event.message);
- };
- elemById("_time").value = "-";
- t0 = new Date().getTime();
- worker.postMessage(parseInt(elemById("_num_rects").value));
- },
- false
- );
- }
- </script>
- </head>
- <body>
- <p>Press button "calc" to get pi</p>
- <p>
- <input type="input" size="10" id="_num_rects" value="100000000">
- <input type="button" id="_piWorker" value="calc">
- pi=<input type="input" size="30" id="_piValue" value="">
- time elapsed: <input type="input" size="10" id="_time" value="-">
- </p>
- </body>
- </html>
相关推荐
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...