【发布时间】:2011-04-26 00:28:22
【问题描述】:
谁能告诉我为什么我的网络工作者不能工作? 我画了一个运行良好的动画画布。但是当我通过文本框调整它的大小时,它会停止运行,直到 JavaScript 执行。现在,我创建了一个 worker 来承担在不停止画布移动的情况下调整图形大小的任务。我希望它通过获取文本框的值来更新隐藏字段的值,转换为字符串,然后将结果设置为隐藏字段值。为此,我制作了文件。我的意思是 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 个空格,以便格式正确。您可以使用编辑器中的按钮
标签: javascript html