It seems it's not about running one function if the other one is succesful, but after an asynchronous operation is done. In calcRoute
, the route calculation API call is asynchronous; that means the function will return before the directionsService
finishes creating the route. One solution is to modify calcRoute
to take a callback:
function calcRoute(callback) {
var start = document.getElementById('start').value;
var end = "Via Tiburtina 500, Roma";
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.TRANSIT
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
// If a callback was passed, run it
if(typeof callback === "function") {
callback();
}
}
});
}
Then change your function call to pass the callback:
<form onsubmit="calcRoute(function(){ boxappear(); reducemap(); }); return false">
Side note: I wouldn't recommend inlining code in HTML attributes like that, is mixes structure and behavior, and can also become hard to maintain; I suggest looking into other ways of attaching events to DOM elements.