Cronometro javascript

spacca!  9 scarso!  0 versione stampa invia per email scheda utente totale visite  1931
Cronometro javascript, si avvia e si ferma alla pressione di un qualsiasi tasto della tastiera
Vedi l'esempio
richiamo la libreria jquery
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
    
    var start = "00:00.00";
    var go = false;
    var speed = 10;
    var a,b,c,d,e,f,u;
    
    $(document).keypress(function(){
        if(go == false) {            
            start = "00:00.00";
            cnt = setInterval(function() {
                gostart();
            },speed);
        }else{
            go = false;
            clearInterval(cnt);
        }        
    })
    function gostart(){
        go = true;
        a = parseInt(start.charAt(0));
        b = parseInt(start.charAt(1));
        u = ":";
        c = parseInt(start.charAt(3));
        d = parseInt(start.charAt(4));
        e = ".";
        f = parseInt(start.charAt(6));
        g = parseInt(start.charAt(7));
        if(g >= 9) {
            g = 0;
            if(f >= 9) {
                f = 0;
                if(d >= 9) {
                    d = 0;
                    if(c >= 5) {
                        c = 0;
                        if(b >= 9) {
                            b = 0;
                            if(a >= 9) {
                                clearInterval(cnt);
                            }else{
                                a++;
                            }
                        }else{
                            b++;
                        }
                    }else{
                        c++;
                    }
                }else{
                    d++;
                }
            }else{
                f++;
            }
        } else {
            g++;
        }
        start = String(a) + String(b) + String(u) + String(c) + String(d) + String(e) + String(f) + String(g);
        for ( var i = 0; i < start.length; i++ ) {
            $("#s" + i).html(start.charAt(i))
        }
    }
</script> 
<style type="text/css">
    div#counter {
        background-color: #000;
        color: gold;
        font-weight: bold;
        padding:6px;
        font-size: 2.8em;
        font-family: Courier;
        letter-spacing: -4px;
        width: 400px;
        text-align: center;
        border:5px solid #063F86;
    }
    div#counter div {
        padding:6px;
    }
</style> 
<div id="counter">
    <span id="s0">0</span>
    <span id="s1">0</span>
    <span id="s2">:</span>
    <span id="s3">0</span>
    <span id="s4">0</span>
    <span id="s5">.</span>
    <span id="s6">0</span>
    <span id="s7">0</span>
</div> 
 
 

Inserisci un commento

non sei loggato

CAPTCHA