较短的字符串会导致网页冻结

时间:2014-11-17 10:17:49

标签: javascript methods webpage

按下HTML图表按钮时,它会激活createChart()方法。但是,如果textarea中的值不超过25个字符,则网页会冻结。此外,只有字母" a"正在引用该方法。

<script type = "text/javascript">
orangeBar = "./orange.gif";

function createChart()
    {

    alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
    textValue = document.getElementById("text").value;
    max = 0;

    for (i=0;i<alphabet.length;i++) 
    {
        value = numberOf(alphabet[i], textValue);
        if (value > max)
        {
            max = value;
        }
    }

    document.write(max);

}  

function numberOf(letter, string)
{
    count = 0;
    for (i=0; i<string.length; i++)
    {
        if (string[i].toLowerCase() == letter)
        {
            count++;
        }
    }
    return count;
}
</script>

谢谢。我对编码很陌生。我可以回答任何问题。

1 个答案:

答案 0 :(得分:2)

使用for (i=0;i<alphabet.length;i++)时,i成为全局变量。然后,您在i中使用相同的for (i=0; i<string.length; i++),它会更改i函数的createChart()。将var放在i之前,使其成为本地。

for (var i=0;i<alphabet.length;i++)

for (var i=0; i<string.length; i++)