Javascript通过一个按钮关闭该函数

Javascript通过一个按钮关闭该函数,javascript,button,Javascript,Button,我正在尝试做一个简单的游戏,我已经做了一个函数,可以自动增加玩家的积分。该功能不会在开始时运行,但当您按下按钮时,它会运行。但是,我唯一的问题是,我需要一个按钮来关闭它 我的html <button onclick="autoMake(); automake = 1;">Automake Hotdogs - On</button> <button onclick="automake = 0;">Automake Hotdogs - Off</button

我正在尝试做一个简单的游戏,我已经做了一个函数,可以自动增加玩家的积分。该功能不会在开始时运行,但当您按下按钮时,它会运行。但是,我唯一的问题是,我需要一个按钮来关闭它

我的html

<button onclick="autoMake(); automake = 1;">Automake Hotdogs - On</button>
<button onclick="automake = 0;">Automake Hotdogs - Off</button>

[编辑]已更改为正确的比较对象。尽管单击停止按钮仍然不起作用,但您可以将那些
if
语句与(
&&
)组合起来:

请注意,我将
automake=1
更改为
automake==1

=
是一个等式,
=
是一个等式


这应该正确地检查
automake
是否等于
1

if语句将automake变量指定给值1,而不是检查它是否等于1。您需要
automake===1
是,它已进行了更改,尽管关闭按钮不会停止function@user2717118:太好了!请考虑:
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)
                }
            }
        }
    }
};
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)
    }
};