网络工作者无法运行 [英] web worker does not run

查看:67
本文介绍了网络工作者无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.有人可以告诉我为什么我的网络工作者无法正常工作吗?
我画了一个运行良好的动画画布.但是,当我通过文本框调整大小时,它将停止运行,直到执行JavaScript.现在,我创建了一个工作人员来承担在不停止画布移动的情况下调整图形大小的任务.我希望它通过获取文本框的值来更新隐藏字段的值,将其转换为字符串,然后将结果设置为隐藏字段值.为此,我制作了文件.我的意思是html标记中没有JavaScript代码.代码文件如下

Hi. 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. 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. 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();

推荐答案

尝试使用相对路径构造Worker:
Try constructing your Worker with a relative path:
new Worker("setValues.js");



如果那不起作用,则需要进行一些调查:

如果您使用的是Google Chrome浏览器,请按Ctrl + Shift + J打开开发人员工具.

-按网络"选项卡,单击录制"按钮并刷新.检查以确保已加载setValues.js.

-按脚本"选项卡,然后单击右侧工人"项旁边的调试".然后,您将可以在Worker中放置一个断点.

祝你好运!



If that doesn''t work, you need to do some investigation:

If you''re using Google Chrome, press Ctrl+Shift+J to open the developer tools.

- Press the Network tab, click the Record button and refresh. Check to make sure setValues.js is loaded.

- Press the Scripts tab, and click ''Debug'' beside the Workers item on the right. Then you will be able to place a breakpoint in your Worker.

Good luck!


这篇关于网络工作者无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆