dimanche 5 mars 2017

Failed to execute 'postMessage' on 'Window': 2 arguments required, but only 1 present

I currently have a web worker trying to accept 4 parameters.

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




Aucun commentaire:

Enregistrer un commentaire