[英]setInterval executes functions only by the time of the interval ends
I have the next setInterval in JS which emits the test() function only by the time the 5s ends: 我在JS中有下一个setInterval ,它仅在5s结束时才发出test()函数:
let interval = setInterval(() => {
test()
}, 5000)
function test() {
console.log("Test") // will be emitted only in 5s
}
How can I tell the method to run the test() function without waiting? 我如何才能告诉方法无需等待就运行test()函数?
If I understand you right, you want the function to be executed immediately, and then every 5 seconds after that. 如果我理解正确,则希望该函数立即执行,然后每5秒执行一次。 In which case, just call it initially too.
在这种情况下,也可以先调用它。
let interval = setInterval(() => test(), 5000)
test();
test(); //call initially and then after 5 sec
let intervalS = setInterval(() => {
test()
}, 5000)
function test() {
console.log("Test") // will be emitted only in 5s
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.