2013-12-14 36 views
0

我不斷收到錯誤:的JavaScript - 未捕獲的ReferenceError:未定義DIS1 VM535:1(匿名函數)

Uncaught ReferenceError: dis1 is not defined VM535:1

(匿名函數),當我嘗試運行,下面的代碼,不過,我可以」真的要指出問題!看來,如果該功能是在範圍上...

<script src="//code.jquery.com/jquery-1.10.2.min.js"></script> 

<!DOCTYPE html> 

<body onclick="whatareyousingingpatrick();"> 
<span id="money">50</span>$ 
<br> 
<span style="background-color:#c3c3c3;width:1000px;height:25px;overflow:hidden;position:relative;display:block;" id="track"></span> 
<br> 
<span id="divthing" style="position:relative;display:block;"></span> 


<script> 
$(document).ready(function() { 
    money = 50; 
    mycars = {}; 

    function dodat() { 
     var btn = document.createElement("div"); 
     btn.style.width = "25px"; 
     btn.style.height = "25px"; 
     btn.style.backgroundColor = "red"; 
     btn.style.boxShadow = "inset 0px 0px 0px 2px black"; 
     btn.style.position = "absolute"; 
     btn.style.left = "0px"; 
     btn.style.webkitTransition = "opacity 1s"; 
     var numba = Math.round(Math.random() * 50); 
     btn.class = "haha"; 
     btn.id = numba; 
     mycars[numba] = -50; 

     var move = function() { 
      mycars[numba] = mycars[numba] + 1; 
      document.getElementById(numba).style.left = mycars[numba] + "px"; 
     }; 

     setInterval(move, 10); 

     document.getElementById("track").appendChild(btn); 
    } 

    setInterval(dodat, 2000); 

    function dis1() { 
     $("shooter").css("background-color", "red"); 
     setTimeout('$("shooter").css("background-color", "blue");', '1000'); 
    compareEl = $("#shoot1"); 
     // Let's find the closest block! 
     var otherEls = $('div'), 
      compareTop = compareEl.offset().top, 
      compareLeft = compareEl.offset().left, 
      winningScore = Infinity, 
      score, winner, curEl; 

     otherEls.each(function() { 
      // Calculate the score of this element 
      curEl = $(this); 
      score = Math.abs(curEl.offset().left - compareLeft); 
      if (score < winningScore) { 
       winningScore = score; 
       winner = this; 
      } 
     }); 
     document.getElementById(winner.id).style.opacity="0"; 

     money = money+1; 
     document.getElementById("money").innerHTML=""+money+""; 
} 

    function dis2() { 
    compareEl2 = $("#shoot2"); 
     // Let's find the closest block! 
     var otherEls2 = $('div'), 
      compareTop2 = compareEl2.offset().top, 
      compareLeft2 = compareEl2.offset().left, 
      winningScore2 = Infinity, 
      score2, winner2, curEl2; 

     otherEls2.each(function() { 
      // Calculate the score of this element 
      curEl2 = $(this); 
      score2 = Math.abs(curEl2.offset().left - compareLeft2); 
      if (score2 < winningScore2) { 
       winningScore2 = score; 
       winner2 = this; 
      } 
     }); 

     document.getElementById(winner2.id).style.opacity="0"; 
} 


    function dis3() { 
    compareEl3 = $("#shoot3"); 
     // Let's find the closest block! 
     var otherEls3 = $('div'), 
      compareTop3 = compareEl3.offset().top, 
      compareLeft3 = compareEl3.offset().left, 
      winningScore3 = Infinity, 
      score3, winner3, curEl3; 

     otherEls3.each(function() { 
      // Calculate the score of this element 
      curEl3 = $(this); 
      score3 = Math.abs(curEl3.offset().left - compareLeft3); 
      if (score3 < winningScore3) { 
       winningScore3 = score; 
       winner3 = this; 
      } 
     }); 

     document.getElementById(winner3.id).style.opacity="0"; 
} 


function dis4(){ 
    compareEl4 = $("#shoot4"); 
     // Let's find the closest block! 
     var otherEls4 = $('div'), 
      compareTop4 = compareEl4.offset().top, 
      compareLeft4 = compareEl4.offset().left, 
      winningScore4 = Infinity, 
      score4, winner4, curEl4; 

     otherEls4.each(function() { 
      // Calculate the score of this element 
      curEl4 = $(this); 
      score4 = Math.abs(curEl4.offset().left - compareLeft4); 
      if (score4 < winningScore4) { 
       winningScore4 = score; 
       winner4 = this; 
      } 
     }); 

     document.getElementById(winner4.id).style.opacity="0"; 
} 
}); 

original = 0; 
function whatareyousingingpatrick(){ 
if(money >= 50){ 
money = money-50; 
original = original+1; 
    setInterval("dis"+original+"();", 2500); 
     var btn = document.createElement("shooter"); 
     btn.style.display = "block"; 
     btn.id = "shoot"+original+""; 
     btn.style.height = "25px"; 
     btn.style.width = "25px"; 
     btn.style.backgroundColor = "blue"; 
     btn.style.borderRadius= "100%"; 
     btn.style.boxShadow= "0px 0px 0px 100px rgba(250, 250, 250, 0.7);"; 
     btn.style.position = "absolute"; 
     btn.style.left = event.pageX; 
     btn.style.top = event.pageY; 
     document.getElementById("divthing").appendChild(btn); 
} 
else{ 
alert("Sorry, this dude costs over 50 bucks."); 
} 
} 
</script> 

回答

3

disX功能在ready回調中定義的,但setInterval評估在全球範圍的字符串。要麼在全局範圍內定義函數,要麼將函數引用直接傳遞給setInterval

+0

謝謝堆人 – user3092778

相關問題