2012-02-29 122 views
0

我写一个应用程序,你可以玩彩票,如果你摇动手机,6个随机数字将填写你的彩票。获取随机数并填充它们的函数崩溃。Javascript方法崩溃 - 我只是不明白为什么

这是我的document.ready

$(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上测试,所有最新版本) 任何帮助非常感谢。

+0

看起来你翻译你的代码从德文到英文,但错过了for循环中的某些东西。请仔细检查错别字。 (专业提示:总是用英文代码) – pduersteler 2012-02-29 08:39:47

+0

小提琴:http://jsfiddle.net/xaKGB/1/ – 2012-02-29 08:41:03

+0

我会改变它的。我知道很多人告诉我用英文编码:) – dan 2012-02-29 08:43:14

回答

3

我从来没有在您的代码中看到isUniqueNumber = true。因此你的功能是无止境的,会崩溃。

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

+0

哦,我的,就是这样。每次他得到一个数字他都没有离开while循环。谢谢!!! – dan 2012-02-29 08:43:53

0

你有变量声明为遵循

var counter = 0; 
var randomNumberArray = []; 

isUniqueNumber变量未申报到你的代码