2016-08-18 75 views
0

想知道如果有人能引导我走向正确的方向,我正在尝试使用Javascript来制作一个小游戏来帮助我学习。从本质上讲,我声明了所有我想要在我的函数外部进行更改的变量,以便它们作用于代码中的全局函数,但if语句似乎没有证明是成功的,因为教程指出我的代码是正确的,请参阅下面的代码;Javascript - if statement error

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

您需要更改d.getTime();StartDate.getTime();以反映变量名称的变化。

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

的jsfiddle:Solution

编辑,包括Xufox的改善。

+0

这可能是'StartTime = new Date()。不需要不必要的变量。 – Xufox

0

尝试从StartGame()函数返回变量Refresh。 它看起来是这样的:你叫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 
};