I currently have a web worker trying to accept 1 argument
wworker = postMessage([textCoords[0], textCoords[1], latlong[0], latlong[1]]);
wworker.addEventListener('message', findDistance);
However, an error keeps popping up on console: "Uncaught TypeError: Failed to execute 'postMessage' on 'Window': 2 arguments required, but only 1 present." AFAIK the postMessage 2nd argument is optional.
and here is my worker.js
var distance;
function findDistance(event) {
var destinationLat, destinationLon, originLat, originLon, haversineLat, haversineLon, thetaLat, thetaLon;
destinationLat = event.data[0] * (Math.PI/180);
destinationLon = event.data[1] * (Math.PI/180);
originLat = event.data[2] * (Math.PI/180);
originLon = event.data[3] * (Math.PI/180);
thetaLat = destinationLat - originLat;
thetaLon = destinationLon - originLon;
haversineLat = (1-Math.cos(thetaLat))/2;
haversineLon = (1-Math.cos(thetaLon))/2;
distance = 2 * 6376 * Math.asin(Math.sqrt(haversineLat + Math.cos(originLat) * Math.cos(destinationLat) * haversineLon));
postMessage(distance);
}
self.addEventListener('message', findDistance);
You are not calling .postMessage()
on Worker
; also you are attaching a new message
event at each iteration of for
loop. Move wworker.addEventListener('message', findDistance);
outside of for
loop, call wworker.postMessage()
within for
loop
var wworker = new Worker("assets/scripts/webworker.js");
wworker.addEventListener('message', findDistance);
for(i=0;i<lines.length;i++){
var temp = lines[i].split(",");
wworker.postMessage([textCoords[0], textCoords[1], latlong[0], latlong[1]]);
textCoords.shift();
textCoords.shift();
}