可能有人能告訴我爲什麼我的網絡工作者不起作用? 我畫了一個運行良好的動畫畫布。但是當我通過文本框調整它的大小時,它會停止運行,直到執行JavaScript。現在,我創建一名工作人員來承擔調整圖形大小的任務,而不會停止畫布的移動。我希望它更新隱藏字段的值,通過取值的文本框,轉換爲字符串,然後將結果以隱藏字段的值。爲此,我製作了文件。我的意思是在HTML標記中沒有JavaScript代碼。代碼文件如下網絡工作者不起作用
/* Code that will be run by the worker */
function applyChanges(radius, size) {
return radius + "," + size;
}
/*
Add an event listener to the worker, this will be called when
the worker receives a message from the main page.
*/
this.onmessage = function (event) {
var data = event.data;
// Message sent by the worker to the main page.
postMessage(applyChanges(data.radius, data.size));
}
/* Worker's code */
// Create a new worker
var myWorker = new Worker("C:\applications\bb\scripts\setValues.js");
/*
Add a event listener to the worker, this will be called whenever the worker posts any message.
*/
myWorker.onmessage = function (event) {
document.getElementById().value = event.data;
};
// Register events for button.
document.getElementById("button").onclick = function() {
var circle = document.getElementById("tcircle");
var square = document.getElementById("tsquare");
var radius = circle.value;
var size = square.value;
// check if those are numerics
if (!isNaN(radius) && !isNaN(size)) {
// verify that the won't hide more the 1/4 of the circle.
if (radius >= size/Math.SQRT2) {
// since we are going to test scrolling and zooming, we are not going to set max values of radius and size.
message = { "tcircle": radius, "tsize": size };
// Message sent by the main page to the worker.
myWorker.postMessage(message);
}
else {
alert("The radius must not be less that: size/sqrt(2)");
}
}
else {
alert("Required numeric type!");
}
}
// Terminate the worker.
myWorker.terminate();
請縮進代碼4個空格以便正確格式。你可以使用編輯器中的按鈕 – mpen 2011-04-26 00:30:22