【问题标题】:log a nested loop element every second for at least a minute? in Javascript?每秒记录一个嵌套循环元素至少一分钟?在 Javascript 中?
【发布时间】:2022-01-20 15:37:45
【问题描述】:
let innerArrayOne = [1,"red","fed"]

let innerArrayTwo = [2,"blue","you"]

let innerArrayThree = [3,"green","bean"]

let innerArrayFour = [4,"yellow","fellow"]

let arrayS = [innerArrayOne,innerArrayTwo,innerArrayThree, innerArrayFour]

这是我迄今为止尝试过的

for(let i = 0; i < arrayOfDoom.length; i++){
      console.log("----")
      console.log(arrayOfDoom[i])
      console.log("----")
      for(let j = 0; j < arrayOfDoom[i].length;j++)
      console.log(arrayOfDoom[i][j])
    }
}

这是我制作的测试数据。这是我到目前为止所尝试的。这为您提供了数组中的每个元素。


function loop(count, callback, done) {
  let counterForLoop = 0;


  
  let next = function () {setTimeout(iteration, 500);};
  
  let iteration = function () {
      if (counterForLoop < count) { callback(counterForLoop, next);
      } else { done && done();}
   
      counterForLoop++; 

    }
  iteration();
}

loop(10000, function (i, next) {next();})


loop(9, function (i, nextI) {
  console.log("----")
  console.log(i)
  console.log("----")
  loop(3, function (j, nextJ) {
  console.log(arrayS[i][j]);nextJ();
}, nextI);});

这是我检查过的唯一另一个关闭的 StackOverflow。它有效,但它在第一个数组中的最后一个元素之后结束。我并不完全理解递归,所以每次我尝试编辑时,比如在计数器达到 8 后将计数器设置为零,它都会破坏它。我希望输出是

1 
red 
fed
2
blue
you
....

一个元素出来,每秒一到两分钟,循环一旦结束,它只需要重复。

有什么想法或我应该研究什么?

【问题讨论】:

  • arrayOfDoom 应该是 arrayS 吗?

标签: javascript arrays nested settimeout setinterval


【解决方案1】:

对于所有未来的兄弟,这就是我的工作方式

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}


let i = 0;
let loopCounter = 0;
let randomNumber = 1

const outerLoop = () => {
    for (i; i < arrayS.length;){
      console.log(randomNumber);
      console.log(arrayS[randomNumber][loopCounter])
      i++;
      loopCounter++;
      if(loopCounter === 3){
        loopCounter = 0
        randomNumber = getRandomInt(8)
        
      }
      if(i === 4){
        i = 0
      }
      break;
        }
}
setInterval(outerLoop, 2000);

【讨论】:

    【解决方案2】:

    未来的兄弟们还应该考虑将问题分成两部分:(1) 在固定的时间间隔内做任何事情,以及 (2) 随机记录。

    setInterval 的一点修饰可以提供可以隐藏时间数学并在指定经过的时间后停止。

    也可以改进随机记录,方法是打乱,然后按顺序记录,以获得不重复的随机性。

    let innerArrayOne = [1,"red","fed"];
    let innerArrayTwo = [2,"blue","you"];
    let innerArrayThree = [3,"green","bean"];
    let innerArrayFour = [4,"yellow","fellow"];
    let arrayS = [innerArrayOne,innerArrayTwo,innerArrayThree, innerArrayFour];
    
    // fisher-yates shuffle, thanks to https://stackoverflow.com/a/2450976/294949
    function shuffle(array) {
      let currentIndex = array.length,  randomIndex;
      while (currentIndex != 0) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex--;
        [array[currentIndex], array[randomIndex]] = [
          array[randomIndex], array[currentIndex]];
      }
      return array;
    }
    
    // invoke fn on a set interval until elapsed ms have elapsed
    function repeat(fn, interval, elapsed) {
      fn() // up to author if we start right away
      const start = (new Date()).getTime(); // ms since epoch
      const id = setInterval(() => {
        const now = (new Date()).getTime();
        if (now - start > elapsed) {
          clearInterval(id);
        } else {
          fn()
        }
      }, interval);
    }
    
    let shuffledArray = shuffle(arrayS.slice());
    let index = 0;
    
    repeat(() => {
      console.log(shuffledArray[index])
      if (index < shuffledArray.length-1) index++;
      else {
        shuffledArray = shuffle(arrayS.slice());
        index = 0;
      }
    }, 800, 4800)

    【讨论】:

    • 谢谢你,我会检查你的代码,看看我可以添加什么。你的有一个问题。输出不同。我需要内部循环去 (1 s) 3 (1 s) green (1 s) bean。正在发生的事情是正在向你走来。所以它在 3 处画了一个小盒子,在绿色画了一个中号盒子,在 bean 画了一个大盒子。所以,它随机选择一个盒子扔给你外循环,但内循环必须按顺序关闭。我如何得到它的两个问题是写的。没有超时。第二个选择等待第一个,我希望第二个选择发生在第一个选择的中间
    猜你喜欢
    • 2021-11-14
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2020-07-19
    相关资源
    最近更新 更多