2010-06-22 12 views

risposta

46

ne dite:

var counter = 0; 
var interval = setInterval(function() { 
    counter++; 
    // Display 'counter' wherever you want to display it. 
    if (counter == 5) { 
     // Display a login box 
     clearInterval(interval); 
    } 
}, 1000); 
+9

bella risposta concisa - il conto alla rovescia aggiunto e demo funzionante - http://jsbin.com/igoxo/3/ –

+0

Nice one compagno !! Grazie!! – Tapha

+0

Non devi usare 'clearInterval (intervallo)'? Dovresti chiamare la funzione con la variabile 'interval', non con quella 'counter'. – linkyndy

12

Questo è esattamente il codice che ha funzionato per me:

<p>You'll be automatically redirected in <span id="count">10</span> seconds...</p> 

<script type="text/javascript"> 

window.onload = function(){ 

(function(){ 
    var counter = 10; 

    setInterval(function() { 
    counter--; 
    if (counter >= 0) { 
     span = document.getElementById("count"); 
     span.innerHTML = counter; 
    } 
    // Display 'counter' wherever you want to display it. 
    if (counter === 0) { 
    // alert('this is where it happens'); 
     clearInterval(counter); 
    } 

    }, 1000); 

})(); 

} 

</script> 

<meta http-equiv="refresh" content="10;url=http://www.example.com" /> 

speriamo vi sia utile;)

12

http://jsfiddle.net/brynner/Lhm1ydvs/

HTML

<span class="c" id="5"></span> 

JS

function c(){ 
    var n=$('.c').attr('id'); 
    var c=n; 
    $('.c').text(c); 
    setInterval(function(){ 
     c--; 
     if(c>=0){ 
      $('.c').text(c); 
     } 
     if(c==0){ 
      $('.c').text(n); 
     } 
    },1000); 
} 

// Start 
c(); 

// Loop 
setInterval(function(){ 
    c(); 
},5000); 
+2

5 anni dopo, ma comunque ottima risposta;). – Tapha

+1

L'ho svalutato, ottima risposta! –