Javascript方法崩溃 - 我只是不明白为什么

时间:2012-02-29 08:33:57

标签: javascript jquery

我正在写一个你可以玩彩票的应用程序,如果你摇动手机,你的彩票上会有6个随机数字填写。获取随机数并填充它们的函数崩溃。

这是我的文件。已经

$(document).ready(function() {  
                    //initialising some variables, not related here
        //...

                    //Clickhandler for a number clicked
        $('#ticket_detail .tipfield .tipbox ul li').click(function() 
            {clickNumber(this); 
        });
                    //testmethod to test the crashing method
        testRandomNumbers();

    })

在javascript文件中,有测试方法

 function testRandomNumbers(){

    //invokes crashing numbers many times - causes crash
    for (var z = 0; z<10;z++){

        randomNumbers();

    }



}

...调用崩溃方法

function randomNumbers(){ 

    //counter for 6 random numbers
counter = 0;

    //empties the lottery, method is listed below
clearAllNumbers();

    //array to save the numbers
randomNumberArray = [];



//6 runs for 6 random numbers
while (counter < 6){ 

        //get the random number
        randomNumber = Math.floor((Math.random()*49)+1);



        //check the number with an array to make sure it is not there already - 6 numbers have to be unique
        for (var j = 0; j < 6; j++){

            if (randomNumberArray[j]==randomNumber){

                isUniqueNumber = false;

                break;

            } 

        }

        randomNumberArray[counter]=randomNumber;  



        //if number is Unique, get the next number
        if (isUniqueNumber){ 

            counter++;

        } 

  } 


//after it, go through the array with the random numbers and put graphics on the numbers in the lottery ticket with jQuery
for (var q = 0;q<randomNumberArray.length;q++){

    clickNumber($('#ticket_detail .tipfield .tipbox ul li').filter(function(){return $(this).html() == randomNumberArray[q];}));

}




}

clearAllNumbres()方法

    function clearAllNumbers(){

    $('#ticket_detail .tipfield .tipbox ul li img').remove(); 

    $('#ticket_detail .tipfield .tipbox ul li').removeClass('selected');

    lottoNumbers = [];

}  

所以这就是我所做的一切。 6次运行,而循环时间6次运行循环,我做了一些变量赋值和布尔运算。为什么方法会崩溃?任何想法

编辑:您可能想知道所有变量的初始化位置。在document.ready()或javascript文件

之上初始化

编辑:我知道我们都喜欢堆栈跟踪。可悲的是,我没有得到一个错误,为什么它崩溃。我计算了randomNumbers()方法运行的次数,并且它在第二次或第三次运行后大部分都崩溃了。浏览器无法加载页面并很快显示错误消息(在chrome,opera和firefox上测试,所有最新版本) 非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

我从未在您的代码中看到isUniqueNumber = true。因此,你的功能是无穷无尽的,并会崩溃。

如果未设置变量,if(isUniqueNumber)也将返回false

答案 1 :(得分:0)

您已将变量声明为

var counter = 0;
var randomNumberArray = [];

isUniqueNumber 变量未声明为您的代码