0

I need to update my javascript array in Web worker thread. I cannot accces my javascript array in Web worker thread.

My code is :

self.onmessage = function(event) {
var array = new Uint8Array(event.data);
var sum = 0;
var temparray = new Array();

for(var list = 0; list < array.length; list++ ){
    var temp = myMethod(array[list]); //some operation
    availableArray.push(temp);
}

 self.postMessage("success");
}   

I am getting this error: availableArrayis undefined availableArray.push(temp);

2
  • looks like you didn't declare availableArray, is there a availableArrays = [] somewhere? Commented Jun 23, 2014 at 10:28
  • Yes I defined availableArray in some other js file which already doing some operation. I need to update that array from worker thread. Commented Jun 24, 2014 at 3:28

1 Answer 1

1

You define 2 variables that are arrays (or array-like objects): var array = new Uint8Array and var temparray = new Array, but then in the loop you use a variable that isn't declared anywhere availableArray, I suspect you want to change that variable to temparray.
The error message makes perfect sense: availableArray is not defined anywhere. You're using it as an array, and invoking the push method on it. However, JS, by default, creates a new variable for you whenever you use a var that hasn't been declared. The default value for a non-initialized variable is, of course, undefined. undefined doesn't have a push method, hence the error.

However, just a slight remark, though: in JS new Array is actually discouraged. Best use the array literal notation:

var temparray = [];

It's shorter and safer.

Sign up to request clarification or add additional context in comments.

2 Comments

But I defined availableArray in some other js file. I need to update that array from my worker thread. Is this possible to update an array or any variable which already doing some operation?
@Prasath: A worker isn't a pure thread as you'd have in C/C++. It does not share state, but runs completely separate from the main JS thread. If you want to update the availableArray, you'll have to send a message to the main JS process, and update the data there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.