HTML Web Worker 和 Jquery Ajax 调用
我想知道是否可以在 Web Worker 文件中使用 jQuery。 Google Chrome 给我这个错误:“Uncaught ReferenceError:$未定义”。
这是代码: 父文件:
var loader = new Worker(BASE_URL + "js/rss_loader_worker.js");
// Ask the worker to start loading the RSS from the server
loader.postMessage("loadRss");
// When receive the response from the server
loader.onmessage = function (event) {
console.log(event.data);
}
工作文件:
onmessage = function (event) {
if (event.data === "loadRss") {
loadRss();
}
}
/**
* This function handles the AJAX request to the server side
* then pass the content to the view page
* @param none
* @return html text
*/
loadRss = function () {
$.ajax({
data: {city: CITY_LOCATION},
url: BASE_URL + "/getfeeds",
onsucess: function (data) {
}
});
}
请帮忙,谢谢:)
I'm wondering if I can use jQuery inside the web worker file. Google Chrome gives me this error: "Uncaught ReferenceError: $ is not defined".
Here is the code:
The parent file:
var loader = new Worker(BASE_URL + "js/rss_loader_worker.js");
// Ask the worker to start loading the RSS from the server
loader.postMessage("loadRss");
// When receive the response from the server
loader.onmessage = function (event) {
console.log(event.data);
}
The worker file:
onmessage = function (event) {
if (event.data === "loadRss") {
loadRss();
}
}
/**
* This function handles the AJAX request to the server side
* then pass the content to the view page
* @param none
* @return html text
*/
loadRss = function () {
$.ajax({
data: {city: CITY_LOCATION},
url: BASE_URL + "/getfeeds",
onsucess: function (data) {
}
});
}
Please help, thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,你不能。无法访问非线程安全组件或 DOM,并且您必须通过序列化对象将特定数据传入和传出线程。所以你必须非常努力地工作才能在你的代码中引起问题。 jQuery 是一个 JavaScript DOM 库。
但是您可以在您的工作线程中使用本机
XMLHttpRequest
。并且,导入外部脚本不会通过带有
script
标记的页面:使用 importScripts()< /a> 在你的工作文件中。No you cannot. There's no access to non-thread safe components or the DOM and you have to pass specific data in and out of a thread through serialized objects. So you have to work really hard to cause problems in your code. jQuery is a JavaScript DOM library.
But you can use a native
XMLHttpRequest
in your worker however.And, importing external scripts does not go via the page with a
script
tag : use importScripts() for that in your worker file.这是我发现的:
http://www.html5rocks.com/en/tutorials/ workers/basics/#toc-enviornment-loadingscripts
或
尽管如此,您无法加载 jQuery,因为 jQuery 需要 DOM 访问权限,而 Web Workers 没有该权限。
Here's what I found:
http://www.html5rocks.com/en/tutorials/workers/basics/#toc-enviornment-loadingscripts
or
Although, you cannot load jQuery, because jQuery requires DOM access, which web workers don't have.
由于 Web Worker 位于外部文件中,因此它们无权访问以下 JavaScript 对象:
因此,您不能在 Worker 文件中使用 $ 。更好的是,您可以使用传统的 AJAX 类似
参考 http://www.w3schools.com/html/ html5_webworkers.asp
Since web workers are in external files, they do not have access to the following JavaScript objects:
So you can't use $ inside worker file. Better you can use traditional AJAX something like this
Reference at http://www.w3schools.com/html/html5_webworkers.asp
jQuery 主要用于处理 DOM 和网页。所以它不太适合无法访问 DOM 的 Web Workers。
您可能想使用实用程序库而不是 DOM 库,例如 underscore.js,或者也许有人应该制作一个 jQuery Worker 库,一个 jQuery 的精简版,没有所有 DOM 操作功能,只保留实用函数。
jQuery is mostly for working with the DOM and working with web pages. So it is not so suitable for Web Workers that do not have access to the DOM.
You might want to use a utility library instead of a DOM library, such as underscore.js, or perhaps someone ought to make a jQuery Worker library, a stripped down light version of jQuery without all the DOM manipulation functionality, just keeping the utility functions.
Node.JS 中的执行环境也缺少原生 DOM 实现。我认为可以公平地说 Node.JS 和 HTML5 Web Workers 共享某些限制。
有多种方法可以模拟 DOM 实现,以便在 Node.JS 中使用 jQuery。如果您仍然想在 Web Workers 中使用 jQuery,我认为您应该搜索 Node.JS 解决方案并查看它们是否适用。
The execution environment in Node.JS also lacks a native DOM implementation. I think it's fair to say that Node.JS and HTML5 Web Workers share certain restrictions.
There are ways to simulate a DOM implementation for the purpose of using jQuery in Node.JS. If you still want to use jQuery in Web Workers, I think you should search for the Node.JS solutions and see if they apply.
看看这个插件 https://github.com/rwldrn/jquery-hive
take a look on this plug-in https://github.com/rwldrn/jquery-hive