这是一个新问题。如果没有输入任何内容或文字,我想让我的楠说“这不是一个数字”。我知道物理代码,但我不知道在哪里放或者我必须做一个功能。我把它放在哪里循环并说出先输入的是什么,然后“这不是数字”或“这不是数字”然后NaN或它根本不起作用。
javacript:
window.onload = function (){
var myDiv = document.getElementById("wrapper");
wrapper.setAttribute("align","center");
var startButton = document.getElementById("start");
var pauseButton = document.getElementById("pause");
var secondsBlock = document.getElementById("seconds");
var timer = document.getElementById("timer");
var newTime = document.createElement("h1");
newTime.innerHTML = secondsBlock.value;
timer.appendChild(newTime);
//setTimeout(function countdown () {
var numbers = (newTime.innerHTML);
newTime.innerHTML = numbers;
var newNumber;
function countdown(count){
newNumber =secondsBlock.value;
var numbers = (newNumber);
newTime.innerHTML = numbers;
stopper = setInterval(reduceAndShow, 1000);
if (numbers <10){
newTime.style.color = "red";
newTime.innerHTML = "0" + numbers;
};
};
function reduceAndShow(){
newNumber--;
var numbers = (newNumber);
newTime.innerHTML = numbers;
stopCountdown();
if (newNumber <10){
newTime.style.color = "red";
newTime.innerHTML= "0" + newNumber;
};
};
function stopCountdown(){
if(newNumber <= 0){
clearInterval(stopper);
};
};
startButton.onclick = function(){
countdown();
};
function pauseBut(){
clearInterval(stopper);
};
pauseButton.onclick =function(){
pauseBut()
};
html:
<div id="wrapper">
Seconds: <input type="text" name= "seconds" id="seconds"> <input type="button"
value="Start" id="start"> <input type="button" value="Pause" id="pause">
<div id="extraText"></div>
<div id="timer"></div>
</div>
答案 0 :(得分:2)
你应该让你的例子在JSFiddle.net中运行,这样人们就可以看到整个事情,并调查你为什么不工作。
我已将您的代码更改为以下内容。主要区别在于我将其分为两个功能。一个检查输入是有效数字,另一个检查倒计时/显示。我改变的另一件小事是你宣布倒计时函数的方式,你有两次声明它,我已经改变它所以它只被声明一次:
window.onload = function(){
var myDiv = document.getElementById("wrapper");
var startButton = document.getElementById("start");
va r pauseButton = document.getElementById("pause");
var secondsBlock = document.getElementById("seconds");
var timer = document.getElementById("timer");
var newTime = document.createElement("h1");
function countdown(count) {
newTime.innerHTML = count;
if (count < 10) {
newTime.style.color = "red";
};
timer.appendChild(newTime);
if (count > 0) {
setTimeout(function () {
countdown(count - 1);
}, 1000);
}
}
function testInput() {
var count = seconds.value;
if (count == "" || isNaN(count)) {
newTime.innerHTML = "This is not a number";
timer.appendChild(newTime);
} else if (count > 100) {
newTime.innerHTML = "Number must be between 0 and 99"
timer.appendChild(newTime);
} else {
countdown(count);
}
}
startButton.onclick = testInput;
}
答案 1 :(得分:0)
看起来像一个非常简单的
案例if (typeof(seconds.value) == "number") { newTime.innerHTML = seconds.value; }
else { newTime.innerHTML = "This is not a number"; }
timer.appendChild(newTime);