[英]Javascript turn the function off with a button
我想做一个简单的游戏,我已经做了一个功能,自动增加玩家的积分。 该功能不会在开始时运行,但是当您按下按钮时它会运行。 我唯一的问题是,无论是按一个按钮关闭它。
我的HTML
<button onclick="autoMake(); automake = 1;">Automake Hotdogs - On</button>
<button onclick="automake = 0;">Automake Hotdogs - Off</button>
我的Javascript
var auto = 1;
var automake = 0;
function autoMake() {
if (automake === 1) {
if (bread >= automake) {
if (hotdog >= automake) {
if (sauce >= automake) {
(function loop(timer) {
setTimeout(function() {
assembleClick(auto);
loop(100);
}, timer)
})(100)
}
}
}
}
};
[编辑]改为正确的比较事物。 虽然单击停止按钮仍然无法正常工作
您可以将这些if
语句与逻辑AND运算符 ( &&
)组合使用:
function autoMake() {
if (automake === 1 && bread >= automake && hotdog >= automake && sauce >= automake) {
(function loop(timer) {
setTimeout(function() {
if (automake === 1){
assembleClick(auto);
loop(100);
}
}, timer)
})(100)
}
};
请注意,我将automake = 1
更改为automake === 1
。
这应该正确检查automake
等于1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.