簡體   English   中英

setInterval僅在間隔結束時執行函數

[英]setInterval executes functions only by the time of the interval ends

我在JS中有下一個setInterval ,它僅在5s結束時才發出test()函數:

let interval = setInterval(() => {
  test()
}, 5000)


function test() {
  console.log("Test") // will be emitted only in 5s
}

我如何才能告訴方法無需等待就運行test()函數?

如果我理解正確,則希望該函數立即執行,然后每5秒執行一次。 在這種情況下,也可以先調用它。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM