繁体   English   中英

Javascript用一个按钮关闭该功能

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM