[英]Javascript countdown timer with 05 instead of 5 seconds?
I have created this one in JavaScript! 我已经用JavaScript创建了这个! It is a timer counting down time from 14:59 to 00:00 minutes.
它是一个计时器,用于从14:59到00:00分钟的倒计时时间。 What I need to do is prevent seconds from going into one digit.
我需要做的是防止秒数变成一位数。
For example: 例如:
14:05 - allowed and needed to achieve 14:05-允许和需要实现
14:5 - not allowed and I need to get rid of this situation. 14:5-不允许,我需要摆脱这种情况。
<html>
<head>
<script type="text/javascript">
var interval;
var minutes = 14;
var seconds = 05;
window.onload = function () {
countdown('atskaite');
}
function countdown(element) {
interval = setInterval(function () {
var el = document.getElementById(element);
if (seconds == 0) {
if (minutes == 0) {
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
el.innerHTML = minutes + ':' + seconds;
seconds--;
}, 1000);
}
</script>
</head>
<body>
<div id="atskaite"></div>
</body>
</html>
Any help, please? 有什么帮助吗?
快捷方便:
minutes + ':' + ("0"+seconds).slice(-2);
只需在分配el.innerHTML
之前添加一个检查,就像这样:
if (seconds < 10) seconds = '0' + seconds;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.