0

这是一个新问题。如果没有输入任何内容或文本,我想让我的 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>
4

2 回答 2

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;
}
于 2013-03-05T14:47:30.713 回答
0

看起来像一个非常简单的案例

if (typeof(seconds.value) == "number") { newTime.innerHTML = seconds.value; }
else { newTime.innerHTML = "This is not a number"; }
timer.appendChild(newTime);
于 2013-03-05T14:32:49.593 回答