简体   繁体   English

如何计算JavaScript中for循环内的时间总和

[英]how to calculate the sum of time inside a for loop in javascript

I'm using a function from google api to draw a way on the map it works good heres the mehode: 我正在使用Google api的函数在地图上画出一种方法,它在这里很好用:

calculateAndDisplayRoute(directionsService, directionsDisplay) {

var waypts = [];
// var jsonData = JSON.parse(this.city);

if (!this.isArrayEmpty(this.stations)) {

  for (var i = 0; i < this.stations.length; i++) {

    waypts.push({
      location: this.stations[i].station,
      stopover: true
    });

  }
}

directionsService.route({
  origin: this.depart,
  destination: this.arrivee,
  waypoints: waypts,
  optimizeWaypoints: false,
  travelMode: 'DRIVING'
}, function (response, status) {
  if (status === 'OK') {
    directionsDisplay.setDirections(response);
    var route = response.routes[0];

     let momo;

    // For each route, display summary information.
    for (var i = 0; i < route.legs.length; i++) {
      var routeSegment = i + 1;
    alert('coco');

       let dt = new Date(route.legs[i].duration.value*1000);
       let hr = dt.getHours();
       let m = "0" + dt.getMinutes();
       let s = "0" + dt.getSeconds();
      let durationn= hr+ ':' + m.substr(-2) + ':' + s.substr(-2); //this gives 02:35:45 for exemple
      /*
      //I tried this this code in comment but it doesnt work.
       let time1=durationn;
       momo=time1;

         let [hours1, minutes1, seconds1] = time1.split(':');
         let [hours2, minutes2, seconds2] = momo.split(':');
      momo=moment({ hours: hours1, minutes: minutes1, seconds: seconds1 })
   .add({ hours: hours2, minutes: minutes2, seconds: seconds2 })
   .format('h:mm:ss')
   */

       console.log('mm'+ route.legs[i].start_address + '  '+route.legs[i].end_address +' '+route.legs[i].distance.text+' '+route.legs[i].duration.text);


      console.log( momo);


    }
  } else {
    window.alert('Directions request failed due to ' + status);
  }
});

  }

but my proble is on the duration, i will explain, i want every time this fuction get called, i need the duration to be sumed(sum of the duration). 但是我的问题在于持续时间,我将解释,我希望每次调用此功能时,我都需要对持续时间求和(持续时间的总和)。 I tried this this code in comment but it doesnt work. 我在注释中尝试了此代码,但没有用。 PS: please dont lose your time to understand the code just focus what is inside the second for loop block. PS:请不要浪费您的时间来理解代码,而只需关注第二个for循环块中的内容。

You can do something like that: 您可以执行以下操作:

 var sum = {h: 0, m: 0, s: 0}; var allTimes = ["02:35:45", "11:40:30", "12:55:39"]; allTimes.forEach(time => sum = sumUp(sum, time)); // Just sum up hour, minute and second console.log(parseSum(sum)); // Do formatting in the end function sumUp(sum, time) { var times = time.split(":"); sum.h += +times[0]; sum.m += +times[1]; sum.s += +times[2]; return sum; } function parseSum(sum) { var totSec = sum.s; var s = totSec % 60; var totMin = sum.m + parseInt(totSec/60); var m = totMin % 60; var totHour = sum.h + parseInt(totMin/60); var h = totHour; return `${h}:${m}:${s}`; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM