Saturday, 7 September 2013

Calculating total time from google maps travel time function

Calculating total time from google maps travel time function

I have a time calculation function presented below:
function computetime(result) {
var time=0;
var mytravelroute=result.routes[0];
for (i = 0; i < mytravelroute.legs.length; i++) {
time += mytravelroute.legs[i].duration.value;
}
var totalSec = time;
var hours = parseInt( totalSec / 3600 );
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10
? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds :
seconds);
if (document.getElementById("time").innerHTML == "") {
document.getElementById("time").innerHTML = result;}
else {
if (document.getElementById("time1").innerHTML == "")
{document.getElementById("time1").innerHTML = result;}
else {document.getElementById("time2").innerHTML = result;}
};
}
This function is called 3 times, and each time its results are displayed
in 3 different spots (time, time1, time2). I would like to make a total
time display, but I am not sure how to approach this.
I think the optimal way would be to add up the "time" variable each time
this function is called and then format it the same way this function does
formatting. I am not sure how to get an external variable to capture the
"time" variable value each time the function is called. Could anyone
suggest a way of doing this? Or any other way I could add up the times
outputted by this function?
Thank you very much

No comments:

Post a Comment