这是一个新问题。如果没有输入任何内容或文本,我想让我的 Nan 说“这不是数字”。我知道物理代码,但我不知道把它放在哪里,或者我是否必须创建一个函数。无论我把它放在哪里,它要么循环并说出首先输入的内容,然后是“这不是一个数字”或“这不是一个数字”,然后是 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>