为什么我会收到错误"太多的递归"?

use*_*637 0 html javascript recursion firefox

我正在这里开展一个小项目,我得到这个消息"太多的递归",显然没有那么多.

这是相关的HTML代码

<div id="speed">
    <label for="spe">Input speed</label>
    <input type="text" id="spe" onkeydown="if (event.keyCode == 13){ javascript:CalcBySpeed(); return false; }" />
    <input type="button" name="Sumbit" value="Submit" onclick="javascript:CalcBySpeed()" />
</div>
Run Code Online (Sandbox Code Playgroud)

和一张桌子,通常看起来像这样:

<tr>
    <td id="50">
        <p id="117">5</p>
    </td>
    <td id="51">
        <p id="118">20</p>
    </td>
    <td id="52">
        <p id="119">5</p>
    </td>
    <td id="53">
        <p id="120">1,2</p>
    </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

我的JavaScript函数是:

function CalcBySpeed() {
    var input = "a";
    input = parseFloat(document.getElementById("spe").value) * 100;
    if (input < 5089) {
        getValueBySpeed(input - 3);
    } else {
        alert("Value undefined!");
    }
};

function getValueBySpeed(input) {
    if (document.getElementById(input) != null) {
        document.getElementById(input).style.backgroundColor = "yellow";
        document.getElementById(input + 1).style.backgroundColor = "yellow";
        document.getElementById(input + 2).style.backgroundColor = "yellow";
        document.getElementById(input + 3).style.backgroundColor = "yellow";
        document.getElementById("res1").innerHTML = document.getElementById(input + 3).innerHTML;
        document.getElementById("res2").innerHTML = document.getElementById(input + 2).innerHTML;
        document.getElementById("res3").innerHTML = document.getElementById(input + 1).innerHTML;
        document.getElementById("res4").innerHTML = document.getElementById(input).innerHTML;
    } else {
        getValueBySpeed(input++);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我决定输入我的输入字段:

  • 1.2,js成功突出显示此特定行
  • 1.19 js成功突出显示了同一行
  • 0.97 js成功突出显示同一行

但是当我输入时:

  • 1.1 它登录控制台 too much recursion

我相信当我输入时0.97会有更多的递归,但问题是当我在1.09和之间输入数字时1.16.

在这个范围之外似乎没有问题.

Hal*_*yon 8

什么时候显然没那么多.

显然有;)

您的代码中可能存在错误:

function getValueBySpeed(input) {
    if (document.getElementById(input) != null) {
        ...
    }
    else {
        getValueBySpeed(input++);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果document.getElementById(input) != nullfalse它可能会维持false和功能getValueBySpeed将被调用,直到你得到的递归误差.


作为一个小调:我不明白是什么getValueBySpeed,名字很奇怪,论点很奇怪,实现很奇怪.

我希望也许(根据名称):

function getValueBySpeed(speed) {
    var value;
    value = // get value somehow
    return value;
}
Run Code Online (Sandbox Code Playgroud)