I'm currently diving into the realm of web workers and need to offload some heavy computation tasks without blocking the main browser thread.
At the moment, I have an AJAX call that fetches an XML data structure with multiple data blocks structured like this:
<data>
<sub1>
<sub2>
<sub3>
</data>
<data>
<sub1>
<sub2>
<sub3>
</data>
Next, I convert this structure into a JavaScript object using the following code:
var node = $(xml).find(data);
Afterwards, I serialize the object for transfer to the worker as shown below:
var toPass = JSON.stringify(node);
Subsequently, I send the variable (toPass) to the worker in this manner:
worker.postMessage(toPass);
Everything seems to be running smoothly up until this point. However, I've encountered difficulties when attempting to access the data within the worker.
Inside the worker function, I make an effort to parse the received data like so:
onmessage = function (oEvent) {
var node = JSON.parse(oEvent.data);
for(var i = 0; i < node.length; i++){
var sub1 = node[i].find('sub1').text();
}
};
The primary issue arises when trying to retrieve the "sub1," "sub2," and "sub3" data elements.
It's clear to me that my approach to accessing the data is flawed, particularly with my usage of XML.find method after parsing to a JSON object.
Could someone guide me on the correct way to access the data elements within "node[i]"?
Thanks in advance!