简体   繁体   English

网络工作者无法正常工作

[英]web worker doesn't work

May someone could tell me why my web worker does not work? 有人可以告诉我为什么我的网络工作者无法正常工作吗? I draw an animated canvas that is run well. 我画了一个运行良好的动画画布。 But when I resize it via text boxes, it stops running until the JavaScript executed. 但是,当我通过文本框调整大小时,它将停止运行,直到执行JavaScript。 Now, I create a worker to assume the task of resize the graphic without stopping the movement of canvas. 现在,我创建一个工作人员来承担在不停止画布移动的情况下调整图形大小的任务。 I want it to update the value of the hidden field, by taking the values the text boxes, convert to string, then set the result to the hidden field value. 我希望它通过获取文本框的值来更新隐藏字段的值,转换为字符串,然后将结果设置为隐藏字段值。 For that I make to files. 为此,我制作了文件。 I mean no JavaScript code in the html markup. 我的意思是html标记中没有JavaScript代码。 the code files are the follow 代码文件如下

/* 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 Workers are asynchronous JavaScript processing environments that do not have access to their host environment: the DOM. Web Worker是异步JavaScript处理环境,无法访问其宿主环境:DOM。 In web workers you can offload intense algorithms, Math computations, but you cannot access form elements, change or access the DOM, and I also believe you cannot spawn ajax requests. 在Web Worker中,您可以卸载繁琐的算法,数学计算,但是您无法访问表单元素,更改或访问DOM,而且我也相信您无法生成Ajax请求。

There has been an update to what Chrome allows now that may help with this problem. Chrome现在允许的功能已有更新,可能会解决此问题。

Based on this answer, using Chrome you can pass the data back to the worker, then write it back to the canvas, so others may follow, but at least it is a way to continue with your testing. 根据此答案,您可以使用Chrome将数据传递回工作人员,然后再将其写回到画布上,以便其他人遵循,但这至少是继续进行测试的一种方法。

Web Workers and Canvas 网络工作者和画布

You may want to look at how this demo works to get an idea what you can do with WebWorker and Canvas . 您可能需要看一下该演示的工作原理,以了解使用WebWorkerCanvas可以做什么。

http://www.robodesign.ro/coding/html5-demo-video-histogram/index-web-worker.html http://www.robodesign.ro/coding/html5-demo-video-histogram/index-web-worker.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM