Chronometer + Calculation in seconds

Asked

Viewed 1,519 times

2

I have the code of a chronometer and I would like your help to implement a calculation, I will try to explain as best I can.

inserir a descrição da imagem aqui

Time code:

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>

http://pastebin.com/5i5W9Sfx

  • You need help on what part ? You can’t implement the account is this ?

2 answers

1

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

A suggestion would be so:

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: https://jsfiddle.net/wyvtw2x0/

  • Sergio, I can’t thank you enough, that’s what I’ve been trying to do for weeks, thank you very much. I will ask one more favor if it is not too much trouble, for the deviation calculation to be exact, I have to make the following calculation "the value of TMA /214-1" , another detail, has as the result of the deviation appear the %symbol. I also noticed that in internet explorer does not run the code only in firefox. thanks again.

  • I seem to be learning, edited this line >>> deviation.innerHTML = tmaVal / 214 - 1 . toFixed(2); <<< now missing to adjust the decimal places, as it returns thus ex: 0.02691588785046717, and put the symbol of %

  • @exact silvagno, the .toFixed(2) is to limit to 2 decimal places. To add the symbol % can do desvio.innerHTML = (tmaVal / 214 - 1).toFixed(2) + ' %';

  • @silvagno ele calcula, but maybe the order of magnitude is wrong. See here: https://jsfiddle.net/uuqdz971/2/ Should it be x100? I do not know what account is this to calculate the deviation but the numbers are there and the account happens

  • @silvagno gives me the idea that you should multiply by 100 https://jsfiddle.net/uuqdz971/3/ will not be that?

  • @silvagno 0.01672897 x 100 is 1.67...

  • Sergio, that’s right, thank you very much.

  • @silvagno great! if you want you can mark the answer as accepted.

  • Sure, Sergio, thanks again :)

  • @silvagno: here you can see how and why mark as accepted: http://meta.pt.stackoverflow.com/q/1078/129

Show 5 more comments

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 it wishes to calculate

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

Callback

calc_times.calc();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.