0
A
回答
0
<span class="countdown" rel="30">0:30</span><br/>
<span class="countdown" rel="60">1:00</span><br/>
<span class="countdown" rel="1800">30:00</span><br/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
// Initialization
$(document).ready(function(){
// Replace <span class="countdown"> rel content with the expiry epoch time
var date = new Date(); // This gives you an epoch date in milliseconds
$('span.countdown').each(function(){
// We do rel*1000 to convert to milliseconds, cause rel is in seconds
$(this).attr('rel', date.getTime()+parseInt($(this).attr('rel'))*1000);
});
// Set an interval so updateCountdown() is called every second
setInterval('updateCountdown()', 1000);
});
// Update, called every second
function updateCountdown() {
var date = new Date(); // This gives you an epoch date in milliseconds
// For each <span class="countdown">
$('span.countdown').each(function(){
// Get time left in milliseconds
var timeLeft = parseInt($(this).attr('rel')) - date.getTime();
// Convert from milliseconds to seconds
timeLeft = Math.round(timeLeft/1000);
// Set to 0 if negative
if (timeLeft < 0) timeLeft = 0;
// Extract minutes and seconds for display
var secs = timeLeft % 60;
var mins = (timeLeft-secs)/60;
// Change <span> content
$(this).text(mins+':'+(secs<10?'0':'')+secs);
});
}
</script>
1
您可以使用JavaScript來實現,從上述ANS影響
標記
<body>
<div id="countdown"></div>
</body>
的Javascript
function countdown(remain) {
var countdown = document.getElementById("countdown"),
timer = setInterval(function() {
countdown.innerHTML = (remain%60 < 10 ? "0": "") + remain %60;
if (--remain < 0) { clearInterval(timer); }
},1000);
}
countdown(20);
相關問題
- 1. 建設時間倒計時
- 2. 倒計時時間的Android
- 3. 定時器倒計時時間格式
- 4. 定時器,倒計時,時間
- 5. 倒計時使用圖像
- 6. Vue2:瞬間JS倒計時
- 7. JS倒計時不倒計時
- 8. 修復倒計時倒數計時器
- 9. 倒計時 - iPhone倒數計時器
- 10. Javascript日期和時間倒計時
- 11. javascript倒計時時間格式化
- 12. 倒數計時器,不顯示時間
- 13. Javascript開放時間倒計時
- 14. 時間跨度減秒錶倒計時
- 15. StopCoroutine後重置倒計時時間
- 16. db中多行的倒計時時間
- 17. 的JavaScript/jQuery的時間倒計時
- 18. 時間戳倒計時進入負面
- 19. jquery比較unix時間和倒計時
- 20. 實時倒計時
- 21. 倒計時計時器
- 22. Scanf倒計時
- 23. android倒計時
- 24. Vb.net倒計時
- 25. 秒倒計時
- 26. 倒計時器
- 27. 從倒計時
- 28. 倒計時
- 29. C#倒計時
- 30. jQuery倒計時
。如何在每小時之前添加日期計數? – 2010-11-20 17:25:34
替換「var secs = timeLeft%60; var mins =(timeLeft-secs)/ 60;」部分是由提取小時和天的東西組成。 – 2010-11-24 15:57:52