Javascript - if语句错误
Javascript - if statement error
想知道是否有人可以引导我进入正确的方向,我正试图使用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";
}
}
需要将d.getTime();
改为StartDate.getTime();
,以反映变量名的变化
function StartGame() {
StartTime = new Date().getTime();
Refresh = "Active";
}
JSFiddle:解决方案
编辑,包括Xufox的改进
尝试从StartGame()
函数返回变量Refresh
。它看起来像这样:
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
刷新变量在调用StartGame()后变得可访问。你不能访问FunctionB1中的Refresh变量,因为它还没有被声明。试试这样
function StartGame() {
Refresh = "Active";
}
function FunctionB1() {
if (Refresh == "Active"){
console.log('done');
}
}
function Game() {
StartGame()
FunctionB1()
console.log(Refresh) // Active
};
相关文章:
- 将错误与if语句混淆
- 如何知道javascript for语句中的所有结果是否都是错误的
- MVC Jquery-按钮点击-获取if语句错误
- 由 if / else 语句中的无效语法导致的语法错误
- Javascript else-if语句错误
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- 筛选器后的行计数错误(使用if语句)
- Simple Javascript if语句中存在语法错误
- Null记录错误地触发Else语句
- Javascript if else语句OR条件错误
- 如果语句返回错误答案
- 防止 if 语句中的错误“未定义”
- 使用 If else if else 语句得到错误
- ES6 大箭头符号函数给出语法错误:缺失;在语句之前
- 非法返回语句错误
- 为什么我的 if 语句出现语法错误
- 为什么以下语句执行没有错误
- 我的“if”语句中包含中断的错误(wrox ch 3 练习 4)
- 为什么在调用丢失的函数时会捕获错误,而不是在错误语句中捕获错误
- jQuery无线电检查命中错误语句