Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Created November 15, 2010 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robotlolita/700371 to your computer and use it in GitHub Desktop.
Save robotlolita/700371 to your computer and use it in GitHub Desktop.
// your function
function calcDistance (start, end) {
var distance=9999999;
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
distance = result.routes[0].legs[0].distance.value;
//document.getElementById('distance').innerHTML = "Distance: " +
// distance + " meters";
// return the distance:
}
});
return distance;
}
// -----------------------------------------
// how JavaScript executes it
function calcDistance(start, end) ->
let distance be 9999999
start a request for the content in the `request` object.
return the distance // at this point, the request you initiated in the previous line hasn't returned yet
someDistance = calcDistance(0,1) // 9999999, because return is always executed before the request has a chance of begin run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment