我如何在数组的每个元素上执行一个函数,每隔2秒重复一次?

huangapple go评论56阅读模式
英文:

How do I execute a function for every element in an array with a 2 second delay and then repeating

问题

我正在尝试编写一个具有以下步骤的程序。
每个步骤都涉及在数组上进行迭代,每次迭代之间都有延迟。但是,我希望这三个主要步骤在前一个步骤完成之前不执行,这意味着前一个步骤已经迭代完其所有元素。

  1. 打印出数组中的数字
    //2秒延迟
  2. 从数组中删除奇数
    //2秒延迟
  3. 打印出剩余的数字

我希望每个步骤在前一个步骤完成并延迟2秒后执行。

我还希望每个数字在前一个数字打印后1秒钟后打印,如下所示

const array = [1,2,3,4,5,6,7,8,9,10];

//预期输出

//第一个过程
1
//1秒延迟
2
//1秒延迟
3
//1秒延迟

10
//1秒延迟

//第一个过程结束 2秒延迟

//第二个过程从数组中删除奇数

//第二个过程结束 2秒延迟

//第三个过程打印数组中所有剩余的数字

0
//1秒延迟
2
//1秒延迟
4
//1秒延迟

10
//1秒延迟
英文:

I am trying to write a program with the following steps.
Each step involves iterating over an array with delay between each iteration. However, I want the three main steps themselves to not execute until the previous step has finished, meaning that the previous step has iterated over all of its elements.

  1. print out numbers in array
    //2second pause
  2. remove odd numbers from array
    //2second pause
  3. print out remaining numbers in array

I want each step to execute after the previous one has finished and 2 a second delay.

I also want each number to print to the console 1 second after the previous number has printed like so

const array = [1,2,3,4,5,6,7,8,9,10];

//expected output

//first process
1
//1 sec delay
2
//1 sec delay
3
//1sec delay
…
10
//1sec delay

//first process ends 2 sec delay

//second process remove odd numbers from array

//second process ends 2 sec delay

//third process print all remaining numbers in array

0
// 1 sec delay
2
// 1 sec delay
4
//1 sec delay
…
10
//1 sec delay


答案1

得分: 0

function delay(t) {
  return new Promise(function(resolve) {
    setTimeout(resolve, t);
  });
}
let item = [1, 2, 3];

function executeWithDelay(f) {
  return Promise.all([f(item), delay(2000)])
}

executeWithDelay(firstProcess)
  .then(_ => executeWithDelay(secondProcess))
  .then(_ => executeWithDelay(thirdProcess))

function firstProcess(item) {
  console.log('firstProcess')
}

function secondProcess(item) {
  console.log('secondProcess')
}

function thirdProcess(item) {
  console.log('thirdProcess')
}
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function delay(t) {
  return new Promise(function(resolve) {
    setTimeout(resolve, t);
  });
}
let item = [1, 2, 3];


function executeWithDelay(f) {
  return Promise.all([f(item), delay(2000)])
}

executeWithDelay(firstProcess)
  .then(_ =&gt; executeWithDelay(secondProcess))
  .then(_ =&gt; executeWithDelay(thirdProcess))

function firstProcess(item) {
  console.log(&#39;firstProcess&#39;)
}

function secondProcess(item) {
  console.log(&#39;secondProcess&#39;)
}

function thirdProcess(item) {
  console.log(&#39;thridProcess&#39;)
}

<!-- end snippet -->

I don't know if it is what you want but this basically use Promise.all to resolve your function with a minimum delay of 2s everytime. Then it is just a promise chaining. You just have to implement your functions1/2/3 to print the number with a setTimeout (and return the promise !). You should be able to modify what i gave you to make it work for your example 我如何在数组的每个元素上执行一个函数,每隔2秒重复一次?

huangapple
  • 本文由 发表于 2023年6月15日 12:24:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定