• User

    [JS] Un semplice cronometro

    Sto cercando un semplicissimo cronometro in JS.
    Basta che gestisca il conteggio di Ore-minuti-secondi, partendo da 00:00:00.

    Non ne ho trovati altri, se non questo che però non riesco a correggere (c'è un problema sull'ora)

    Avete qualche script da consigliarmi o riuscite a sistemare questo qua sotto?

    <script language="JavaScript">
     
    var timercount = 0;
    var timestart  = null;
     
    function showtimer() {
        if(timercount) {
            clearTimeout(timercount);
            clockID = 0;
        }
        if(!timestart){
            timestart = new Date();
        }
        var timeend = new Date();
        var timedifference = timeend.getTime() - timestart.getTime();
        timeend.setTime(timedifference);
     var minutes_passed = timeend.getMinutes();
     if (minutes_passed==0) minutes_passed =59;
        if(minutes_passed <10){
            minutes_passed = "0" + minutes_passed;
        } 
     var hours_passed = timeend.getHours(); // l'ora qui vale subito "1"...
        if(hours_passed <10){
            hours_passed = "0" + hours_passed;
        }
        var seconds_passed = timeend.getSeconds();
        if(seconds_passed <10){
            seconds_passed = "0" + seconds_passed;
        }
        document.timeform.timetextarea.value = hours_passed + ":" + minutes_passed + ":" + seconds_passed;
        timercount = setTimeout("showtimer()", 1000);
    }
     
    function sw_start(){
        if(!timercount){
        timestart   = new Date();
        document.timeform.timetextarea.value = "00:59:00";
        timercount  = setTimeout("showtimer()", 1000);
        }
        else{
        var timeend = new Date();
            var timedifference = timeend.getTime() - timestart.getTime();
            timeend.setTime(timedifference);
            var hours_passed = timeend.getHours();
            var minutes_passed = timeend.getMinutes();
     
      if(hours_passed <10){
                hours_passed = "0" + hours_passed;
            }
      if(minutes_passed <10){
                minutes_passed = "0" + minutes_passed;
            }
            var seconds_passed = timeend.getSeconds();
            if(seconds_passed <10){
                seconds_passed = "0" + seconds_passed;
            }
     
            var milliseconds_passed = timeend.getMilliseconds();
            if(milliseconds_passed <10){
                milliseconds_passed = "00" + milliseconds_passed;
            }
            else if(milliseconds_passed <100){
                milliseconds_passed = "0" + milliseconds_passed;
            }
        }
    }
     
    function Reset() {
        timestart = null;
        document.timeform.timetextarea.value = "00:00:00";
    }
     
    </script>
    <form name="timeform">
    Time: <input type=text name="timetextarea" value="00:00" size="10" style = "font-size:20px">
    <br>
    <br>
    <input type=button name="start" value="Start/Lap" onclick="sw_start()">
    <input type=button name="reset" value="Reset" onclick="Reset()">
    </form>