Mobile 5‎ > ‎

WebWorkers





本サンプルはOS2.1, OS 2.2以降での実行が必要です。
現在のDroidget SDKのバージョンでは動作しません。


Configの編集



src/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<widget id="UAAgent_1281148337204" version="1.0" width="300" height="200"> 
<content src="index.html" type="text/html"/>
<name>Web Workers</name>
<description>Web Workers Test</description>
<author url="http://www.gclue.com"></author>
<icon src="icon.png"/>
<license>(C)2010 GClue K.K</license>
</widget>



JavaScriptの作成



src/js/WebWorkers.js
var worker1 = new Worker("js/worker.js");
var worker2 = new Worker("js/worker.js");


worker1.onmessage = function(event) {   
    alert("worker1 msg [" + event.data + "]");
};
worker1.onerror = function(error) {   
    alert(error);
};

worker2.onmessage = function(event) {    
    alert("worker2 msg [" + event.data + "]");
};
worker2.onerror = function(error) {   
    alert(error);
};
function run() {
    worker1.postMessage("Worker1");
    worker2.postMessage("Worker2");
}

src/js/worker.js
onmessage = function(event) { 
    var msg = event.data;
    var result = "parent [" + msg + "]"
    postMessage(result); 
};

onerror = function(e) {
    postMessage(e);
};

index.htmlの作成



src/index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta name="viewport" content="width=240" /> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <title>WebWorkers</title>
    <script type="text/javascript" src="js/WebWorkers.js"></script> 
</head>
<body>
<input type="button" id="formBtn1" value="run" onclick="run()"/> 
</body>
</html>
Comments