[英]web worker doesn't work
有人可以告訴我為什么我的網絡工作者無法正常工作嗎? 我畫了一個運行良好的動畫畫布。 但是,當我通過文本框調整大小時,它將停止運行,直到執行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();
Web Worker是異步JavaScript處理環境,無法訪問其宿主環境:DOM。 在Web Worker中,您可以卸載繁瑣的算法,數學計算,但是您無法訪問表單元素,更改或訪問DOM,而且我也相信您無法生成Ajax請求。
Chrome現在允許的功能已有更新,可能會解決此問題。
根據此答案,您可以使用Chrome將數據傳遞回工作人員,然后再將其寫回到畫布上,以便其他人遵循,但這至少是繼續進行測試的一種方法。
您可能需要看一下該演示的工作原理,以了解使用WebWorker
和Canvas
可以做什么。
http://www.robodesign.ro/coding/html5-demo-video-histogram/index-web-worker.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.