For循环中如何根据Index/iteration个数动态改变间隔时间?
How to Change Interval Time Dynamically in For Loop According to Index/iteration Number?
因为无法发表评论,只好写了这篇post。我得到下面的代码 delays/waits 恰好 1 秒或 1000 毫秒 -
let n = 5;
for (let i=1; i<n; i++)
{
setTimeout( function timer()
{
console.log("hello world");
}, i*1000 );
}
但是我怎样才能延迟它 i*1000 秒而不是固定的 1000 毫秒所以等待取决于迭代次数?
例如,如果 n= 5 ,那么我希望循环在第一次迭代中延迟 1 秒。第二次迭代 2 秒,依此类推。最终延迟为 5 秒。
使用递归调用而不是 for 循环
let i=1;
function a(i) {
if (i > 5)
return
else
b("message", i)
}
function b(s, f) {
setTimeout(function timer() {
console.log(s + " " + f + " seconds");
}, f * 1000);
a(++i);
}
a(i);
我花了一些时间来解读你的问题 xD,但这是你想要的吗?
这将持续触发 console.log,每次延迟 i*1000。
所以第一次是 1 秒长 (1*1000),接下来是 2 秒,依此类推。
let i = 0;
loop = () => {
setTimeout(() => {
console.log(new Date()); // for clarity
i++;
if (i < 10) {
loop();
}
}, i * 1000)
};
loop();
循环不等待超时功能完成。
因此,当循环 运行 时,它会为每个索引安排警报。
您可以使用一个函数,该函数将 运行 根据您的索引但同时安排。你能感受到3秒的不同。
function test(i){
setTimeout( function timer(){
console.log("hello world" + i);
}, i*3000);
}
for (let i=1; i<4; i++) {
test(i);
}
您可以尝试使用 async/await(Promises)来序列化您的代码:
const waitSeconds = seconds => new Promise(resolve => setTimeout(resolve, seconds))
async function main () {
let oldDate = new Date()
let newDate
/*
* If you put 'await' inside the loop you can synchronize the async code, and simulate
* a sleep function
*/
for (let i=1; i<5; i++) {
await waitSeconds(i*1000)
newDate = new Date()
console.log(`Loop for i=${i}, elapsed=${moment(newDate).diff(oldDate, 'seconds')} seconds`)
oldDate = newDate
}
console.log('End')
}
main()
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
这是一个函数,它会立即显示,然后 1 秒后、2 秒后、3 秒后等等。不需要特殊的数学运算,不需要承诺
const n = 5;
let cnt=0;
function show() {
console.log("call "+cnt,"delay: ",cnt,"sec");
cnt++;
if (cnt > n) return; // we are done
setTimeout(show, cnt*1000 ); // cnt seconds later
}
show()
虽然这个任务可以通过 promises、反应流和其他很酷的工具来解决(嘿,还没有人建议使用 worker!),它也可以通过一些算法来解决。
所以你想要按顺序超时:1s,前一个+2s,前一个+3s,等等。这个序列是:1, 3, 6, 10, 15...,它的公式是a[n] = n * (n + 1) / 2
。知道...
let n = 6;
console.log(new Date().getSeconds());
for (let i = 1; i < n; i++) {
setTimeout(function timer() {
console.log(new Date().getSeconds());
}, 1000 * i * (i + 1) / 2);
}
因为无法发表评论,只好写了这篇post。我得到下面的代码 delays/waits 恰好 1 秒或 1000 毫秒 -
let n = 5;
for (let i=1; i<n; i++)
{
setTimeout( function timer()
{
console.log("hello world");
}, i*1000 );
}
但是我怎样才能延迟它 i*1000 秒而不是固定的 1000 毫秒所以等待取决于迭代次数?
例如,如果 n= 5 ,那么我希望循环在第一次迭代中延迟 1 秒。第二次迭代 2 秒,依此类推。最终延迟为 5 秒。
使用递归调用而不是 for 循环
let i=1;
function a(i) {
if (i > 5)
return
else
b("message", i)
}
function b(s, f) {
setTimeout(function timer() {
console.log(s + " " + f + " seconds");
}, f * 1000);
a(++i);
}
a(i);
我花了一些时间来解读你的问题 xD,但这是你想要的吗?
这将持续触发 console.log,每次延迟 i*1000。 所以第一次是 1 秒长 (1*1000),接下来是 2 秒,依此类推。
let i = 0;
loop = () => {
setTimeout(() => {
console.log(new Date()); // for clarity
i++;
if (i < 10) {
loop();
}
}, i * 1000)
};
loop();
循环不等待超时功能完成。 因此,当循环 运行 时,它会为每个索引安排警报。
您可以使用一个函数,该函数将 运行 根据您的索引但同时安排。你能感受到3秒的不同。
function test(i){
setTimeout( function timer(){
console.log("hello world" + i);
}, i*3000);
}
for (let i=1; i<4; i++) {
test(i);
}
您可以尝试使用 async/await(Promises)来序列化您的代码:
const waitSeconds = seconds => new Promise(resolve => setTimeout(resolve, seconds))
async function main () {
let oldDate = new Date()
let newDate
/*
* If you put 'await' inside the loop you can synchronize the async code, and simulate
* a sleep function
*/
for (let i=1; i<5; i++) {
await waitSeconds(i*1000)
newDate = new Date()
console.log(`Loop for i=${i}, elapsed=${moment(newDate).diff(oldDate, 'seconds')} seconds`)
oldDate = newDate
}
console.log('End')
}
main()
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
这是一个函数,它会立即显示,然后 1 秒后、2 秒后、3 秒后等等。不需要特殊的数学运算,不需要承诺
const n = 5;
let cnt=0;
function show() {
console.log("call "+cnt,"delay: ",cnt,"sec");
cnt++;
if (cnt > n) return; // we are done
setTimeout(show, cnt*1000 ); // cnt seconds later
}
show()
虽然这个任务可以通过 promises、反应流和其他很酷的工具来解决(嘿,还没有人建议使用 worker!),它也可以通过一些算法来解决。
所以你想要按顺序超时:1s,前一个+2s,前一个+3s,等等。这个序列是:1, 3, 6, 10, 15...,它的公式是a[n] = n * (n + 1) / 2
。知道...
let n = 6;
console.log(new Date().getSeconds());
for (let i = 1; i < n; i++) {
setTimeout(function timer() {
console.log(new Date().getSeconds());
}, 1000 * i * (i + 1) / 2);
}