简体   繁体   English

如何通过单击按钮打开/关闭功能?

[英]How to turn on/off a function on a button click?

I have been working in a mini-game-project (Simons game) that many of you may know.我一直在做一个你们很多人可能都知道的迷你游戏项目(西蒙斯游戏)。 Where the computer plays a random sequence of buttons in which players have to follow to go to the next level in the game eg: [one click first round, two clicks second round..].计算机播放一系列随机按钮,玩家必须按照这些按钮进入游戏的下一个级别,例如:[第一轮点击一次,第二轮点击两次......]。

I already did all the button effects as well as make the machine plays buttons randomly in a range of ten rounds.我已经做了所有的按钮效果,并让机器在十轮范围内随机播放按钮。 So, what I would like to do is use a button to turn on and off the function that makes the computer clicks By Itself using a button.所以,我想做的是使用一个按钮来打开和关闭使计算机使用按钮自行点击的功能。

I already tried using the jQuery function $(startButton).on('click', clickByItself);我已经尝试过使用 jQuery 函数$(startButton).on('click', clickByItself); alone but it did not worked.独自一人,但没有奏效。

$(document).ready(function() {

  //four variables representing its button effects 
  //button blue effect
  var blueButtonEffect = code here;

  var greenButtonEffect = code here;

  var redButtonEffect = code here;

  var yellowButtonEffect = code here;

  //to be used on the buttonEffects()/clickByItself()
  var arr = [blueButtonEffect, redButtonEffect, greenButtonEffect, yellowButtonEffect];
  let enabled = true;
  let times = 0;
  //makes button effects play itself randomly 
  function clickByItself() {
    let random = Math.floor(Math.random() * arr.length);
    $(arr[random]).click();
    if (++times < 10) {
      setTimeout(function() { clickByItself(times); }, 1000);
    }
  }
  clickByItself();

  function turnOnTurnOff() {
    if (enabled == true) { //TRYING TO TURN ON/OFF THE FUNCTION ON BUTTON CLICK..
      $(startButton).on('click', clickByItself);
    }else{
      $(startButton).on('click', clickByItself);
    }
  }

Now, I am trying to use a function turnOnTurnOff() to see whether I could do the effect of turning on and off with the click of a the startButton .现在,我正在尝试使用函数turnOnTurnOff()来查看是否可以通过单击startButton来实现打开和关闭的效果。 Thank you.谢谢你。

Give this a try:试试这个:

function clickByItself() {
    if(enabled) {
        let random = Math.floor(Math.random() * arr.length);
        $(arr[random]).click();
        if (++times < 10) {
            setTimeout(function() { clickByItself(times); }, 1000);
        }
    }
}
clickByItself();

function turnOnTurnOff() {
    if (enabled) {
        enabled = false;
    } else {
        enabled = true;
        clickByItself();
    }
}

$(startButton).click(function() {
    turnOnTurnOff();
});

You could do it in multiple ways.你可以通过多种方式做到这一点。 One is to use setInterval instead of setTimeout and store it inside a variable.一种是使用setInterval而不是setTimeout并将其存储在变量中。 When you need to stop it, just call clearInterval .当您需要停止它时,只需调用clearInterval

You can use .off() method of jQuery to remove an event listener as follows.您可以使用 jQuery 的.off()方法删除事件侦听器,如下所示。 I added two div s for better demonstration.我添加了两个div以便更好地演示。

One button binds and unbinds (toggles) the click handler of the second button using jQuery's .on() & .off() .使用jQuery的一个键绑定和解除绑定(切换)第二个按钮的点击处理程序.on().off() When the click handler is bound to the second button, clicking it will update the div with a number.当单击处理程序绑定到第二个按钮时,单击它会用一个数字更新div When the click handler is unbounded from the second button, clicking the second button will do nothing.当单击处理程序不受第二个按钮的限制时,单击第二个按钮将不执行任何操作。 Two lines of interest in the JavaScript code below are decorated with a comment each.下面 JavaScript 代码中感兴趣的两行都用注释修饰。 The rest is for demonstration.其余的用于演示。

 window.enabled = false; window.count = 1; // Initialize the view (for demo) $(function() { $('#switchIndicator').html(`<p>${enabled ? 'Switch is ON' : 'Switch is OFF'}</p>`); $('#btn').html(enabled ? 'You can click me :-)' : 'You CANNOT click me'); }); // Toggle click functionality using jQuery's .on() & .off() methods function toggle() { enabled = !enabled; if (enabled) { // Line of interest #1: jQuery .on() $('#btn').on('click', handleClick); } else { // Line of interest #2: jQuery .off() $('#btn').off('click', handleClick); } $('#switchIndicator').html(`<p>${enabled ? 'Switch is ON' : 'Switch is OFF'}</p>`); $('#btn').html(enabled ? 'You can click me :-)' : 'You cannot click me :-(('); $('#btn').removeClass(enabled ? 'disabled' : 'enabled').addClass(enabled ? 'enabled' : 'disabled'); } function handleClick() { $('#counter').append(` ${count++}`); }
 /* CSS */ #btn.enabled { background-color: greenyellow; } #btn.disabled { background-color: lightgray; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="switchIndicator"></div> <button id="switch" onclick="toggle()">On/OFF Switch</button> <button id="btn"></button> <div id="counter"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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