简体   繁体   English

如何遍历一个数组,该数组一次一个地触发数组中每个项目的单击事件

[英]How to loop through an array that triggers an click event on each item in the array one at a time

I am having trouble with something in my function. 我在功能方面遇到了麻烦。 I am trying to trigger click events one after another with a short delay between each item of my array...if anybody can explain to me how to fix It and why. 我试图在我的阵列的每个项目之间一个接一个地触发点击事件...如果有人可以向我解释如何解决它以及为什么。 I will be very grateful for any help. 我将非常感谢任何帮助。

 var track3 = new Audio(); track3.src= 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'; head= $("#head").click (function (){ $("#head").css ("background-color","black"); $("#head").css({ opacity: 0.9}); track3.play (); setTimeout(function(){ $("#head").css({ opacity: 0.0 }); $("#head").css("background-color",""); }, 300); }); shoulders= $("#shoulders").click (function (){ $("#shoulders").css ("background-color","cyan"); track3.play(); setTimeout(function() { $("#shoulders").css("background-color",""); }, 300); }); knees= $("#knees").click (function (){ $("#knees").css ("background-color","mediumPurple"); track3.play (); setTimeout(function(){ $("#knees").css("background-color", ""); }, 300); }); toes= $("#toes").click (function (){ $("#toes").css ("background-color","mediumSpringGreen"); track3.play(); setTimeout(function() { $("#toes").css("background-color", ""); }, 300); }) var round= 0; var player= [ ]; var simon=[ ]; var pat= ["head", "shoulders", "knees", "toes"]; $(".start").click(function (){ simon=[ ]; player=[ ]; round=0; additionalRound(); }) function additionalRound(){ round ++; $("h2").html("Round:" +round); setTimeout(function() { $("h2").html("HEAD, SHOULDERS, KNEES, AND TOES!"); },2660); sequence(); } function sequence (){ simon.push(pat[Math.floor (Math.random ()*4 )]); blinkerBeats (); } function blinkerBeats() { for (var i = 0; i < simon.length; i++) { setTimeout(function() { $(simon[i]).trigger('click'); }, i * 800); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Ok, I edited your question into readable code. 好的,我把你的问题编辑成可读代码。 Here's what are looking for: 这是寻找的东西:

function sequence (){
  simon.push(pat[Math.floor (Math.random ()*4 )]);
  blinkerBeats (simon);
}

function blinkerBeats(arr) {
  var timer = 0,
      interval = 800;
  arr.map(function(elem){
    timer++;
    setTimeout(function(){
      $('#'+elem).trigger('click');
    }, timer * interval)
  })
}

Right now, simon.push() in your sequence() function only adds 1 entry to the array. 现在,你的sequence()函数中的simon.push()只在数组中添加了1个条目。 If you actually want a sequence, you probably want to do: 如果你真的想要一个序列,你可能想要:

function sequence (times){
  for (i = 0; i < times; i++) {
    simon.push(pat[Math.floor (Math.random ()*4 )]);
  }
  blinkerBeats (simon);
}

Working example: Since I don't have your initial markup, I just used some simple <input> s to test the code. 工作示例:由于我没有初始标记,我只是使用了一些简单的<input>来测试代码。 Please note I didn't change the rest of your functions (as I'm not familiar to their logic - I only changes sequence() and blinkerBeats() functions: 请注意我没有更改其余的函数(因为我不熟悉它们的逻辑 - 我只更改sequence()blinkerBeats()函数:

 var track3 = new Audio(); track3.src= 'https://s3.amazonaws.com/freecodecamp/simonSound1.mp3'; head= $("#head").click (function (){ $("#head").css ("background-color","black"); $("#head").css({ opacity: 0.9}); track3.play (); setTimeout(function(){ $("#head").css({ opacity: 0.0 }); $("#head").css("background-color",""); }, 300); }); shoulders= $("#shoulders").click (function (){ $("#shoulders").css ("background-color","cyan"); track3.play(); setTimeout(function() { $("#shoulders").css("background-color",""); }, 300); }); knees= $("#knees").click (function (){ $("#knees").css ("background-color","mediumPurple"); track3.play (); setTimeout(function(){ $("#knees").css("background-color", ""); }, 300); }); toes= $("#toes").click (function (){ $("#toes").css ("background-color","mediumSpringGreen"); track3.play(); setTimeout(function() { $("#toes").css("background-color", ""); }, 300); }) var round= 0; var player= [ ]; var simon=[ ]; var pat= ["head", "shoulders", "knees", "toes"]; $(".start").click(function (){ simon=[ ]; player=[ ]; round=0; additionalRound(); }) function additionalRound(){ round ++; $("h2").html("Round:" +round); setTimeout(function() { $("h2").html("HEAD, SHOULDERS, KNEES, AND TOES!"); },2660); sequence($('#clicks').val()); } function sequence (times){ for (i = 0; i < times; i++) { simon.push(pat[Math.floor (Math.random ()*4 )]); } blinkerBeats (simon); } function blinkerBeats(arr) { var timer = 0, interval = 800; arr.map(function(elem){ timer++; setTimeout(function(){ $('#'+elem).trigger('click'); }, timer * interval) }) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label><input type="checkbox" id="head"> Head</label> <label><input type="checkbox" id="shoulders">Shoulders</label> <label><input type="checkbox" id="knees">Knees</label> <label><input type="checkbox" id="toes">Toes</label> <hr /> Times: <input type="number" value="6" id="clicks" /> <input type="button" value="Start" class="start" /> <h2>Click start!</h2> 

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

相关问题 如何遍历一个jQuery数组,然后一次输出一项(从索引0开始)? - How can I loop through a jquery array and then output each item (starting at index 0) one at a time? 如何循环遍历数组以等待每个项目之间的时间? - How to loop through an array in waiting a time between each item? 循环遍历数组并添加事件侦听器“click”到每个 - Loop through array and add event listener “click” to each 如何做到这一点,以便for循环中的事件侦听器每次单击将一项推入数组 - how to make it so event listener within for loop pushes one item per click to array 如何在每次单击事件侦听器时遍历数组? - How to iterate through an array on each click of an event listener? 无法将事件绑定到数组循环中的每个项目 - Could not bind event to each item in array loop 遍历每个项目的所有数组项目 - loop through all array items for each item 如何遍历mapbox中的图层数组以侦听click事件? - How to loop through array of layers in mapbox to listen for a click event? 如何遍历字符串,每次向数组添加1个额外的字符 - How to loop through string, adding 1 extra character to an array each time for循环返回数组中一个项的单个字母而不是数组中的每个项 - for loop returning individual letters of one item in array and not each item in the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM