Beginner
clideployweb
Timeouts & Intervals
Timers are used to schedule functions to happen at a later time.
Here we create a timer that will print "Hello, World!" to the console after 1 second (1000 milliseconds).
!--frsh-copybutton:1-->
setTimeout(() => console.log("Hello, World!"), 1000);
You can also cancel a timer after it has been created.
!--frsh-copybutton:2-->
const timerId = setTimeout(() => console.log("No!"), 1000);
clearTimeout(timerId);
Intervals can be created to repeat a function at a regular interval.
!--frsh-copybutton:3-->
setInterval(() => console.log("Hey!"), 1000);
Intervals can also be cancelled.
!--frsh-copybutton:4-->
const intervalId = setInterval(() => console.log("Nope"), 1000);
clearInterval(intervalId);
Run this example locally using the Deno CLI:
deno run https://byexample-wwv03xf36j0g.deno.dev/timers.ts
Additional resources: