英文:
How do I execute a function for every element in an array with a 2 second delay and then repeating
问题
我正在尝试编写一个具有以下步骤的程序。
每个步骤都涉及在数组上进行迭代,每次迭代之间都有延迟。但是,我希望这三个主要步骤在前一个步骤完成之前不执行,这意味着前一个步骤已经迭代完其所有元素。
- 打印出数组中的数字
//2秒延迟 - 从数组中删除奇数
//2秒延迟 - 打印出剩余的数字
我希望每个步骤在前一个步骤完成并延迟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.
- print out numbers in array
//2second pause - remove odd numbers from array
//2second pause - 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(_ => executeWithDelay(secondProcess))
.then(_ => executeWithDelay(thirdProcess))
function firstProcess(item) {
console.log('firstProcess')
}
function secondProcess(item) {
console.log('secondProcess')
}
function thirdProcess(item) {
console.log('thridProcess')
}
<!-- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论