2010-06-22 14 views

回答

46

如何:

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

很好的回答言簡意賅 - 加入倒計時,工作演示 - http://jsbin.com/igoxo/3/ – 2010-06-22 01:20:30

+0

尼斯一個隊友!謝謝!! – Tapha 2010-06-22 04:57:46

+0

你不必使用'clearInterval(interval)'?你應該用'interval'變量來調用函數,而不是'counter'變量。 – linkyndy 2011-07-02 22:13:53

12

這正是爲我工作的代碼:

<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" /> 

希望它幫助;)

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年後,但很好的答案);。 – Tapha 2015-01-26 18:25:27

+1

我提高了它,很好的回答! – 2015-06-16 04:26:58

相關問題