Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

HTML Web Worker and Jquery Ajax call

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 :)

question from:https://stackoverflow.com/questions/4838883/html-web-worker-and-jquery-ajax-call

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...