如何在 JS 函数中使用 setTimeout 减慢列表的输出
how to slow down the output of a list using setTimeout inside of JS function
该程序将输出数组中包含的 5 个元素中的 4 个元素的组合。但它会立即执行,我希望它执行得更慢,每行后等待 1 秒。
我已经尝试过很多方法来添加 setTimeout(function()content,1000*i) 它对我不起作用,相关问题也无济于事。任何人都可以帮忙解决吗?
const vegetables = ["carrot", "tomatoes", "potatoes", "celery", "pepper"];
const three = [];
three.length = 4;
function combine(a, b, c) {
if (b === 0) {
console.log(`${three.join(" ")}`);
return;
}
for (let i = c; i <= a.length - b; i++) {
three[three.length - b] = a[i];
combine(a, b - 1, i + 1);
}
}
combine(vegetables, three.length, 0);
我相信你正在寻找的是显示数组列表的延迟效果。您需要做的就是将 setTimeOut 放入循环中,它会完美运行。
const vegetables = ["carrot", "tomatoes", "potatoes", "celery", "pepper"];
const three = [];
three.length = 4;
function combine(a, b, c) {
if (b === 0) {
console.log(`${three.join(" ")}`);
return;
}
for (let i = c; i <= a.length - b; i++) {
setTimeout(()=>{ //------------------------> Place settimeout here
three[three.length - b] = a[i];
combine(a, b - 1, i + 1);
},1000*i)
}
}
combine(vegetables, three.length, 0);
您可以尝试这样的操作:
const vegetables = ["carrot", "tomatoes", "potatoes", "celery", "pepper"];
const three = [];
const combined = [];
three.length = 4;
function sleep(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}
function combine(a, b, c) {
if (b === 0) {
combined.push(`${three.join(" ")}`);
return;
}
for (let i = c; i <= a.length - b; i++) {
three[three.length - b] = a[i];
combine(a, b - 1, i + 1);
}
}
(async function main() {
console.log('started');
combine(vegetables, three.length, 0);
for (const entry of combined) {
console.log(entry);
await sleep(1000);
}
})();
您可以使用for await ... of
const veg = ["carrot", "tomatoes", "potatos", "celery", "pepper"];
const combo = [false, true, true, true, true]; // 4 out of 5
const t0 = performance.now();
function display(){
const t1 = performance.now();
console.log(filter(veg, combo), ` time = ${t1-t0}`);
}
async function* nextPerm(a){
do {
await new Promise(res => setTimeout(res, 1000));
yield a;
} while(nextPermutation(a));
}
let g = nextPerm(combo);
(async function() {
for await (let next_combo of g) {
display();
}
})();
function filter(a, select) {
return a.filter((_e,i) => select[i]);
}
function nextPermutation(array, first = 0, last = array.length-1) {
if(first>=last){
return false;
}
let i = last;
for(;;){
const i1 = i;
if(array[--i]<array[i1]){
let i2 = last+1;
while(array[i]>=array[--i2]);
[array[i], array[i2]] = [array[i2], array[i]];
reverse(array, i1, last);
return true;
}
if(i===first){
reverse(array, first, last);
return false;
}
}
}
function reverse(array, i=0, j=array.length-1) {
while (i < j)
[array[i++], array[j--]] = [array[j], array[i]];
}
该程序将输出数组中包含的 5 个元素中的 4 个元素的组合。但它会立即执行,我希望它执行得更慢,每行后等待 1 秒。 我已经尝试过很多方法来添加 setTimeout(function()content,1000*i) 它对我不起作用,相关问题也无济于事。任何人都可以帮忙解决吗?
const vegetables = ["carrot", "tomatoes", "potatoes", "celery", "pepper"];
const three = [];
three.length = 4;
function combine(a, b, c) {
if (b === 0) {
console.log(`${three.join(" ")}`);
return;
}
for (let i = c; i <= a.length - b; i++) {
three[three.length - b] = a[i];
combine(a, b - 1, i + 1);
}
}
combine(vegetables, three.length, 0);
我相信你正在寻找的是显示数组列表的延迟效果。您需要做的就是将 setTimeOut 放入循环中,它会完美运行。
const vegetables = ["carrot", "tomatoes", "potatoes", "celery", "pepper"];
const three = [];
three.length = 4;
function combine(a, b, c) {
if (b === 0) {
console.log(`${three.join(" ")}`);
return;
}
for (let i = c; i <= a.length - b; i++) {
setTimeout(()=>{ //------------------------> Place settimeout here
three[three.length - b] = a[i];
combine(a, b - 1, i + 1);
},1000*i)
}
}
combine(vegetables, three.length, 0);
您可以尝试这样的操作:
const vegetables = ["carrot", "tomatoes", "potatoes", "celery", "pepper"];
const three = [];
const combined = [];
three.length = 4;
function sleep(ms) {
return new Promise(function (resolve) {
setTimeout(resolve, ms);
});
}
function combine(a, b, c) {
if (b === 0) {
combined.push(`${three.join(" ")}`);
return;
}
for (let i = c; i <= a.length - b; i++) {
three[three.length - b] = a[i];
combine(a, b - 1, i + 1);
}
}
(async function main() {
console.log('started');
combine(vegetables, three.length, 0);
for (const entry of combined) {
console.log(entry);
await sleep(1000);
}
})();
您可以使用for await ... of
const veg = ["carrot", "tomatoes", "potatos", "celery", "pepper"];
const combo = [false, true, true, true, true]; // 4 out of 5
const t0 = performance.now();
function display(){
const t1 = performance.now();
console.log(filter(veg, combo), ` time = ${t1-t0}`);
}
async function* nextPerm(a){
do {
await new Promise(res => setTimeout(res, 1000));
yield a;
} while(nextPermutation(a));
}
let g = nextPerm(combo);
(async function() {
for await (let next_combo of g) {
display();
}
})();
function filter(a, select) {
return a.filter((_e,i) => select[i]);
}
function nextPermutation(array, first = 0, last = array.length-1) {
if(first>=last){
return false;
}
let i = last;
for(;;){
const i1 = i;
if(array[--i]<array[i1]){
let i2 = last+1;
while(array[i]>=array[--i2]);
[array[i], array[i2]] = [array[i2], array[i]];
reverse(array, i1, last);
return true;
}
if(i===first){
reverse(array, first, last);
return false;
}
}
}
function reverse(array, i=0, j=array.length-1) {
while (i < j)
[array[i++], array[j--]] = [array[j], array[i]];
}