typescript 具有可变延迟和等待的 Angular 4 setTimeout()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52110419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 4 setTimeout() with variable delay and wait
提问by Cristina
I have a list of events with timestamp
.
What I want is to display the events based on the timestamp
:
我有一个带有timestamp
. 我想要的是基于以下内容显示事件timestamp
:
To add a delay:
添加延迟:
delay = timestamp(t+1) - timstamp(t)
I know this doesn't work well with setTimeout
, but there is an workaround, if the timeout is constant, in my case is not.
我知道这不适用于setTimeout
,但有一个解决方法,如果超时是恒定的,在我的情况下不是。
Is it possible to make the next setTimeout()
wait for the previous one? To be specific, if the first setTimeout()
has a 5 second delay and the second one has 3 seconds, the second one will appear first. I want them to be in the same order but execute one after the other.
是否可以让下一个setTimeout()
等待前一个?具体来说,如果第一个setTimeout()
有5秒的延迟,第二个有3秒的延迟,那么第二个会先出现。我希望它们的顺序相同,但一个接一个地执行。
This example works for a constant delay, but I want to calculate the delay based on the information I take iterating the list.
此示例适用于恒定延迟,但我想根据我在迭代列表时获取的信息来计算延迟。
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(i);
}, 1000);
}
采纳答案by Rohit Sharma
You can use IIFE(Immediately Invoked Function Expression)and function recursion instead. Like:
您可以改用IIFE(立即调用函数表达式)和函数递归。喜欢:
let i = 0;
(function repeat(times){
if (++i > 5) return;
setTimeout(function(){
console.log("Iteration: " + i);
repeat();
}, 5000);
})();
Live fiddle here.
住在这里小提琴。
回答by nipuna777
When using the latest Typescript or ES code, we can use aync/await for this.
当使用最新的 Typescript 或 ES 代码时,我们可以为此使用 aync/await。
let timestampDiffs = [3, 2, 4];
(async () => {
for (let item of timestampDiffs) {
await timeout(item * 1000);
console.log('waited: ' + item);
}
})();
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
We need to wrap the for loop in an async immediately invoked function because to support await.
我们需要将 for 循环包装在一个异步立即调用的函数中,因为要支持 await。
We also do need a timeout function that returns a promise once the timeout is complete.
我们还需要一个超时函数,一旦超时完成就返回一个承诺。
After that, we wait for the timeout to complete before continuing with the loop.
之后,我们在继续循环之前等待超时完成。
回答by user184994
Don't call it within a loop, as it won't wait for the setTimeout
to complete.
不要在循环中调用它,因为它不会等待setTimeout
完成。
You can instead pass a list of times that you want to wait, and iterate them recursively:
您可以改为传递您想要等待的时间列表,并递归地迭代它们:
let waits = [5000, 3000, 1000];
function setDelay(times) {
if (times.length > 0) {
// Remove the first time from the array
let wait = times.shift();
console.log("Waiting", wait);
// Wait for the given amount of time
setTimeout(() => {
console.log("Waited ", wait);
// Call the setDelay function again with the remaining times
setDelay(times);
}, wait);
}
}
setDelay(waits);
回答by Ashok
You can set time out period using i value dynamically. Like
您可以使用 i 值动态设置超时时间。喜欢
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(i);
}, i*1000);
}
回答by Mattstir
You can think of something like that:
你可以这样想:
setDelay(1,5);
function setDelay(i, max) {
setTimeout(function(){
console.log(i);
if(i < max){
i++;
setDelay(i, max);
}
}, 1000);
}
The idea is to set the next setTimeout(..
in a timeout.
Of course you can modify it to have different long timeouts as well.
这个想法是setTimeout(..
在超时中设置下一个。当然,您也可以将其修改为具有不同的长超时。
Hope i can help :)
希望我能帮上忙:)