2012-02-29 120 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變量未申報到你的代碼