【发布时间】:2014-03-20 13:54:01
【问题描述】:
我正在尝试创建一个倒计时计时器,如果用户输入 1 或 2 小时,如果用户输入 2,则从一个小时开始倒计时,等等。我知道我需要将我从用户那里获得的值乘以 60为了从输入计算分钟和秒切换到按小时计算,但我不太确定在哪里这样做。
<script>
function startTimer() {
userInput = document.getElementById('userTime').value;
if(userInput.length == 0){
alert("Please enter a value");
} else {
var numericExpression = /^[0-9]+$/;
if(!userInput.match(numericExpression)){
alert("Please enter a number")
} else {
function display( notifier, str ) {
document.getElementById(notifier).innerHTML = str;
}
function toMinuteAndSecond( x ) {
return Math.floor(x/60) + ":" + x%60;
}
function setTimer( remain, actions ) {
(function countdown() {
display("countdown", toMinuteAndSecond(remain));
actions[remain] && actions[remain]();
(remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
})();
}
setTimer(userInput, {
10: function () { display("notifier", "Warning message 1", document.bgColor="#ffff00"); },
5: function () { display("notifier", "Warning message 2", document.bgColor="#ff69b4"); },
0: function () { display("notifier", "Final message", document.bgColor="#ff0000"); }
});
}
}
}
</script>
<div id="countdown"></div>
<div id="notifier"></div>
<p>
Please Enter A Time Limit for the Meeting: <input type="text" id="userTime" />
<input type="button" value="Go" onclick="startTimer()" />
</p>
【问题讨论】:
标签: javascript timer countdown