Joh*_*ith -1 javascript jquery if-statement function
这是我的代码:
var count = 2;
var decrementAmount = 1;
function reduceVariable() {
count -= decrementAmount;
}
$("#button").click(function() {
reduceVariable();
});
if (count == 0) {
$("#avrageReactionTime").html("Hello");
};
Run Code Online (Sandbox Code Playgroud)
当我单击我的按钮两次时,具有id avrageReactionTime的div不会更改为具有文本hello.为什么我有这个问题......
现在你测试一次,而不是每次计数器改变时测试.
你必须把if事件处理程序放在内部:
$("#button").click(function() {
reduceVariable();
if (count==0) {
$("#avrageReactionTime").html("Hello");
}
});
Run Code Online (Sandbox Code Playgroud)
请注意,正确缩进代码会使其显而易见.