Chạy hàm ngay khi sử dụng setInterval trong JavaScript

Cập nhật ngày 19/01/2025
Lượt xem: 13

Cách 1

foo();
setInterval(foo, delay);

Cách 2

function foo() {
   // do stuff
   // ...

   // and schedule a repeat
   setTimeout(foo, delay);
}

// start the cycle
foo();

Cách 3

(function foo() {
    ...
    setTimeout(foo, delay);
})();