0
我是新來的Javascript,並與定時器玩了一下。我發現基於測試字段輸入here的倒數計時器的代碼,我試圖向同一頁面添加一個計數器計時器,但它不工作。基於用戶輸入的Javascript倒計時和countup計時器
<html>
<body>
<div id="countdown"></div>
<div id="countup"></div>
<div id="notifier"></div>
<script type="text/javascript">
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 {
var countuptime = 0;
function display(notifier, str) {
document.getElementById(notifier).innerHTML = str;
}
function toMinuteAndSecond(x) {
return Math.floor(x/60) + ":" + x%60;
}
function setTimer(remain, actions, countuptime) {
(function countup() {
display("countup", toMinuteAndSecond(userInput - countuptime));
(countuptime+= 1) <= userInput && setTimeout(arguments.callee, 1000);
})();
(function countdown() {
display("countdown", toMinuteAndSecond(remain));
actions[remain] && actions[remain]();
(remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
})();
}
setTimer(userInput, {
10: function() { display("notifier", "Just 10 seconds to go"); },
5: function() { display("notifier", "5 seconds left"); },
0: function() { display("notifier", "Time is up"); }
});
}
}
}
</script>
Please Enter A Number: <input type="text" id="userTime" />
<input type="button" value="Go" onclick="startTimer()" />
</body>
</html>
基本上,我試圖創建一個從0開始,直到它是一樣的userInput,此時應立即停止上升一個新的變量(countuptime)。但是,當我嘗試測試時,向上計時器顯示NaN:NaN。我正試圖遠離JQuery並獲得Javascript的基礎知識。
啊,傻我。謝謝! – an14