0

I wonder why I'm getting undefined values ?? So basically I'm using google map api to obtain the time traveled between 2 points. When i try to alert the value with

alert(response.routes[0].legs[0].duration.value / 60);

All works fine i got the values, but when i try to assign this value to a variable and alert it, it gave me "undefined". Below is the javascript code

function travellingTime(Origin,Destination){
 var Time;
 var directionsService = new google.maps.DirectionsService();
 var request = {
    origin: Origin,
    destination: Destination,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {

        // Display the distance:
        //alert(response.routes[0].legs[0].distance.value + " meters");

        // Display the duration:
        Time = (response.routes[0].legs[0].duration.value / 60);

        
        
    }
});
alert(Time) ;
//app.setTime(String(Time)) 
}
Nicky Larson
  • 43
  • 2
  • 7
  • 1
    `directionsService.route()` is an asynchronous function. This means that it returns a value at some time in the future. Javascript, however, doesn't wait — you are calling `alert()` before the async function has returned. You need to either call `alert()` from within the callback or define a function that's called when the callback fires. – Mark Mar 14 '18 at 03:23
  • 1
    My best guess is that the `directionsService.route` method is processing things asychronously. The time isn't being set until some time in the future. You are `alert`ing immediately. – Daniel W Strimpel Mar 14 '18 at 03:23
  • @Mark_M I got you you but how to assign this value with a variable ? – Nicky Larson Mar 14 '18 at 03:26
  • @DanielWStrimpel how to remediate this? – Nicky Larson Mar 14 '18 at 03:28
  • I don't know what you are trying to do. You'll have to do any processing using the `Time` variable inside of the code passed to the `directionsService.route` method. – Daniel W Strimpel Mar 14 '18 at 03:37

1 Answers1

0

I found the answer, since it is getting the time asynchronously. I found a way to delayed so as to get the values. I've inserted this code inside the directionservice.route()

setTimeout(function() { 
Time = (response.routes[0].legs[0].duration.value / 60); 
                      }, 0);
Nicky Larson
  • 43
  • 2
  • 7
  • This isn’t the answer. It just puts your variable assignment into a different async fucntion. And what happens if the original async function takes more time? – Mark Mar 14 '18 at 04:32
  • it fails :'( , c'mon man I need help – Nicky Larson Mar 14 '18 at 05:25
  • I can't add an answer to a closed question. I would suggest looking at the thread linked at the top that was suggested as a duplicate. It has tons of good examples and links. The basic problem is that you can't avoid working with async patterns in javascript — you need to embrace them. – Mark Mar 14 '18 at 06:43
  • i tried putting onAjaxSuccess: then assign value still not working – Nicky Larson Mar 14 '18 at 06:52