Chronometer + Calculation in seconds

2

I have the code of a stopwatch and I would like your help to implement a calculation, I will try to explain it as well as possible.

Stopwatchcode:

var segundo = 0 + "0";
var minuto = 0 + "0";
var hora = 0 + "0";

function tempo() {
  if (segundo < 59) {
    segundo++
    if (segundo < 10) {
      segundo = "0" + segundo
    }
  } else
  if (segundo == 59 && minuto < 59) {
    segundo = 0 + "0";
    minuto++;
    if (minuto < 10) {
      minuto = "0" + minuto
    }
  }
  if (minuto == 59 && segundo == 59 && hora < 23) {
    segundo = 0 + "0";
    minuto = 0 + "0";
    hora++;
    if (hora < 10) {
      hora = "0" + hora
    }
  } else
  if (minuto == 59 && segundo == 59 && hora == 23) {
    segundo = 0 + "0";
    minuto = 0 + "0";
    hora = 0 + "0";
  }
  form.cronometro.value = hora + ":" + minuto + ":" + segundo

}
<html>

<body>

  <form name="form">
    <input type="text" name="cronometro" value="00:00:00" readonly="readonly" />
    <br />
    <button type="button" onclick="setInterval('tempo()',983);return false;">Iniciar Cronômetro</button>
  </form>



</body>

</html>

link

    
asked by anonymous 26.09.2016 / 15:44

2 answers

1

I suggest that your internal counter be in seconds. So you can use that number for the calculations and you can always convert it to hh: mm: ss format.

One suggestion would look like this:

var secs = 10760;
var hora = document.getElementById('cronometro')
var chamadas = document.getElementById('chamadas')
var segundos = document.getElementById('segundos')
var tma = document.getElementById('tma')
var desvio = document.getElementById('desvio')
setInterval(cronometro, 1000);


function cronometro() {
    secs++;
    hora.innerHTML = [
        secs / 60 / 60, // horas
        (secs / 60) % 60, // minutos
        secs % 60 // segundos
    ].map(Math.floor).map(
        s => (s < 10) ? '0' + s : s
    ).join(':');
    segundos.innerHTML = secs;
    var tmaVal = secs / parseInt(chamadas.value, 10);
    tma.innerHTML = tmaVal;
    desvio.innerHTML = (((tmaVal / Math.floor(tmaVal - 1)) - 1) * 100).toFixed(2);
}

jsFiddle: link

    
26.09.2016 / 19:17
0

Function to perform the calculation

var calc_times = {
    sign: ''
    , value: []
    , convert: function(  data ){
        var c = String( data ).split(':').map(Number);
        var r = 0;
        if( c[0] == undefined ){
            c[0] = 0;
        }
        r += ( Math.abs(c[0]) * 60 * 60 ) ;
        if( c[1] == undefined ){
            c[1] = 0;
        }
        r += ( Math.abs(c[1]) * 60 ) ;
        if( c[2] == undefined ){
            c[2] = 0;
        }
        r += Math.abs(c[2]) ;
        if( String( data ).indexOf("-") == 0 ){
            r = r * ( -1 ) ;
        }
        calc_times.value.push( r );
    }
    , calc: function(){
        var sec = 0 ;
        calc_times.value.forEach(function(i,o){
            sec += i ;
        })
        if( sec < 0 ){
            calc_times.sign = "-" ;
        }
        return calc_times.sign + [
            Math.abs(sec) / 60 / 60, // horas
            (Math.abs(sec) / 60) % 60, // minutos
            Math.abs(sec) % 60 // segundos
        ].map(Math.floor).map(
            s => (s < 10) ? '0' + s : s
        ).join(':');
    }
};

Informs the values you want to calculate

calc_times.convert('-00:09:01');
calc_times.convert('00:00:03');
calc_times.convert('-00:00:01');

Call to Answer

calc_times.calc();
    
29.06.2018 / 21:55