2013-03-25 92 views
-3

我有這個腳本,即時通訊使用作爲學習的東西。在這個代碼即時通訊嘗試擁有它,以便每次按鈕被點擊時,它將運行多個代碼。比如每次失去1點耐力,改變3件事中的1件(金錢,耐力或健康),然後更新屏幕上的號碼。不幸的是,我的號碼沒有更新。我之前只用一個函數調用了這個程序,但現在我有多個,它引發了問題。函數內的調用函數

<html> 
<head> 
<script> 
var stam=100; 
var cash=0; 
var health=100; 
var rNumber=Math.random(); 
function explore(){  //2<- This function gets called and runs two functions 
    if(stam>1){ 
     redStam(); 
     refresh(); 
       //<-- will add a function to use a random number later to decide whether health, money or stamina gets changed. 
    }; 
    else{alert("You have no stamina")}} 
    function refresh(){ //3<-This function gets called, which refreshes the current variables 
     document.getElementById('number').innerHTML=stam; 
    }; 
    function redStam(){ //3<This function also gets called. 
     stam--; 
    }; 
</script> 
<button onclick="explore()">Explore</button>   1<- button gets clicked 
</head> 
<body> 
<p id='number'>100</P> 
</body> 
</html> 
+5

什麼問題?另外請注意,函數聲明require(),例如,函數redStam(){} – cernunnos 2013-03-25 15:21:28

+1

我開始編輯這個格式,但有太多的語法錯誤,我不確定我會做得更好。 OP,請正確縮進您的代碼,並確保您發佈至少語法上有效的代碼......無論它是否有效。 – BLSully 2013-03-25 15:24:27

+0

只是將問題添加到帖子中..我也剛剛更改了我的代碼以在我的函數中使用(),但代碼仍然無法正常工作 – 2013-03-25 15:24:45

回答

2

您的if聲明有一個不需要的分號:if { ... }; else { ... }

嘗試在遇到JavaScript錯誤時檢查瀏覽器的控制檯。它會告訴你問題出在哪裏。

+0

刪除我的分號後,並添加()在前面的評論中建議,它似乎已被修復。我將考慮使用未來的控制檯,謝謝。 – 2013-03-25 15:31:43

0

您有一堆語法錯誤,請檢查您的控制檯,並在if之後加上分號。

fiddle似乎工作。

function explore() { //2<- This function gets called and runs two functions 
    if (stam > 1) { 
     redStam(); 
     refresh(); 
    } else { 
     alert("You have no stamina") 
    } 
}