2016-08-18 4 views
0

누가 올바른 방향으로 나를 조종 할 수 있는지 궁금하다면 Javascript를 사용하여 배우는 데 도움이되는 작은 게임을 만들려고합니다. 본질적으로 나는 내 함수의 외부에서 변경하고자하는 모든 변수를 선언하여 코드에서 작동하지만 if 문이 성공을 증명하지 못하는 것처럼 보입니다. 튜토리얼이 내 코드를 올바르게 가리키는 것처럼 수정하지 못하는 것 같습니다 아래 코드를 참고하십시오.Javascript - if 문 오류

var Refresh; 
Refresh = "InActive"; 

var Counter; 
Counter = 0; 

var StartTime; 


function StartGame() { 
    var StartDate; 
    StartDate = new Date(); 
    StartTime = d.getTime(); 
    Refresh = "Active"; 
} 


function FunctionB1() { 
    if (Refresh == "Active"){ 
     document.getElementById("Bean1").style.display = "None"; 
     Counter ++; 
     document.getElementById("BeanCount").innerHTML = Counter + " Out of 150"; 
    } 
} 
+1

게시 된 코드의 아무 곳에서나 'var Refresh'가 표시되지 않습니다. –

+0

@GlenDespaux 맨 처음 줄 ... – Teemu

+0

아, 코드 블록에 넣지 않은 것을 알았습니다. 죄송합니다. –

답변

0

당신은 변수 이름의 변경을 반영 StartDate.getTime();d.getTime();을 변경해야합니다.

function StartGame() { 
StartTime = new Date().getTime(); 
Refresh = "Active"; 
} 

JSFiddle : Solution

편집 됨 Xufox의 개선을 포함합니다.

+0

이것은 'StartTime = new Date(). getTime();''Refresh = "Active";'일 수 있습니다. 불필요한 변수가 필요 없습니다. – Xufox

0

변수 RefreshStartGame() 함수에서 반환하려고합니다. 그것은 다음과 같이 보일 것입니다 : 당신은() StartGame를 호출 한 후

function StartGame() { 
    var StartDate; 
    StartDate = new Date(); 
    StartTime = d.getTime(); 
    Refresh = "Active"; 
    return Refresh; 
} 

function FunctionB1() { 
    var startRefresh = StartGame(); 
    if (startRefresh == "Active"){ 
     document.getElementById("Bean1").style.display = "None"; 
     Counter ++; 
     document.getElementById("BeanCount").innerHTML = Counter + " Out of 150"; 
    } 
} 

FunctionB1(); // Call the function 
0

새로 고침 변수에 접근하게된다. 아직 선언되지 않았으므로 FunctionB1에서 Refresh 변수에 액세스 할 수 없습니다. 시험해보십시오

function StartGame() { 
    Refresh = "Active"; 
} 

function FunctionB1() { 
    if (Refresh == "Active"){ 
     console.log('done'); 
    } 
} 

function Game() { 
    StartGame() 
    FunctionB1() 
    console.log(Refresh) // Active 
};