无限循环的问题

Issue with an infinite loop?

本文关键字:问题 无限循环      更新时间:2023-09-26

我遇到了一些意外无限循环的问题。我刚开始使用javascript,所以我一直在尝试制作一个简单的游戏作为练习。我总是陷入无限循环,我也不知道为什么。我很确定这不是语法错误,因为控制台没有告诉我任何错误。下面是出现问题的代码:

HTML:

<input id="lemonadePrice" class="buytextbox" placeholder="Lemonade price">
<input class="submit" onclick="begin();" type="submit" value="Begin!">
JavaScript:

while (isNaN(lemonadePrice)) {
    document.getElementById("introduction").innerHTML="Uh-oh! Your lemonade price is not a
number!Please remove any words or symbols like '$'.";
    lemonadePrice = document.getElementById("lemonadePrice");
}
下面是完整代码的链接:jsfiddle

这可能是因为您的while()循环。当isNAN(lemonadePrice)返回true时,while循环可能会无限地进行下去,这就是在你的例子中发生的情况,假设你说它无限地运行。

您可以尝试使用if()。自。"lemonadePrice"的getElementById将不会返回一个数字,isNAN()将一直求值为true。isNAN()简单地表示"不是一个数字"。如果你想一下,你就知道bug在哪里了。

我认为你正在寻找"lemonadePrice"的值,所以你不应该只获取元素,你还应该考虑拉入它里面的值。我没有在你的代码中看到这个,但它只是这个DOM: var value = document.getElementById(id).value;

我想你需要一个if而不是while

这个定义来自维基百科

在大多数计算机编程语言中,while循环是一个控制流语句,该语句允许基于给定的值重复执行代码布尔条件。while循环可以看作是一个重复的if声明。

表示while循环将在括号内的条件下重复。在本例中,您需要的是一个条件语句(if())

If只执行一次。While将重复执行,直到条件为false。在你的例子中,条件总是为真。这就是为什么你会陷入无限循环。

见下面的代码

if (isNaN(lemonadePrice)) {
   document.getElementById("introduction").innerHTML="Uh-oh! Your lemonade price is not a
   number!Please remove any words or symbols like '$'.";
   lemonadePrice = document.getElementById("lemonadePrice");
}

如果我们假设begin()基本上是这样的:

function begin(){
    var lemonadePrice = document.getElementById("lemonadePrice");
    while (isNaN(lemonadePrice)) {
        document.getElementById("introduction").innerHTML="Uh-oh! Your lemonade price is not a number!Please remove any words or symbols like '$'.";
        lemonadePrice = document.getElementById("lemonadePrice");
    }
}

则循环将永远没有机会退出,因为lemonadePrice总是被设置为lemonadePrice DOM元素的"value";这意味着lemonadePrice始终为NaN,因此循环是无限的。就像其他人已经提到的那样,您需要使用if()语句而不是循环,并删除最后一行。您还需要查看元素的:

function begin(){
    var lemonadePrice = document.getElementById("lemonadePrice").value;
    if(isNaN(lemonadePrice)) {
        document.getElementById("introduction").innerHTML="Uh-oh! Your lemonade price is not a number!Please remove any words or symbols like '$'.";
    }
}

您最好使用onsubmit和防止表单提交,如果无效。最好使用正则表达式来检查它是否是有效的浮点数。

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <form action="yourfile.php" method="post" onsubmit="validate();">
    <input id="lemonadePrice" class="buytextbox" placeholder="Lemonade price" /> <span id="lemonade_error"></span>
    <br>
    <input class="submit" type="submit" value="Begin!" />
</form>
<script type='text/javascript'>//<![CDATA[ 
function validate() {
    var lemonade_price = document.getElementById("lemonadePrice").value;
    if (lemonade_price.match(/'d+'.*'d*/i)){
        document.getElementById("lemonade_error").innerHTML = "Not valid.";
        if (event.preventDefault){
            event.preventDefault();
        } else {
            event.returnValue = false; // for IE as dont support preventDefault;
        }
        return false;
    }
    return true;
}
//]]>  
</script>
</body>
</html>

代码:http://jsfiddle.net/Pxuey/5/