JavaScript Curriculum
Timers & Animation Loop
mediumThe coffee shop menu needs a countdown timer for flash sales, a toast notification that disappears after 3 seconds, and a smooth animated banner. Each needs a different timer approach.
Timers & Animation Loop
JavaScript is single-threaded — it can only do one thing at a time. Timers schedule callbacks for later without blocking the thread. requestAnimationFrame syncs your animations with the display refresh rate.
setTimeout & setInterval
Run code once after a delay, or repeatedly at an interval:
// Use the buttons to fire timers
// setTimeout — run once after delay: const id = setTimeout(() => doSomething(), 2000) clearTimeout(id) // cancel before it fires // setInterval — run repeatedly: const id = setInterval(() => tick(), 1000) clearInterval(id) // always store the id to stop it
Countdown Timer
A real countdown built with setInterval — with pause, resume, and reset:
let count = 10
const id = setInterval(() => {
count--
display.textContent = count
if (count <= 0) {
clearInterval(id)
showSuccess()
}
}, 1000)requestAnimationFrame — Smooth Animation Loop
The browser calls your function before each repaint — typically 60 times per second:
requestAnimationFramesetInterval(fn, 16)function animate(timestamp) {
const elapsed = timestamp - startTime
// Update position based on elapsed time:
el.style.transform =
`translateX(${Math.sin(elapsed/1000) * 100}px)`
requestAnimationFrame(animate) // loop
}
// Start:
let startTime
requestAnimationFrame(ts => {
startTime = ts
requestAnimationFrame(animate)
})
// Stop:
cancelAnimationFrame(rafId)Your Challenge
Build showToast(msg, ms) — creates a div, appends to body, removes after ms using setTimeout. Then add a CSS entry animation. Use requestAnimationFrame to animate the width of a progress bar from 0 to 100% over exactly 2 seconds using timestamp math.
Challenge
Build a toast notification system: showToast(message, duration) creates a .toast element, appends it to the body, and uses setTimeout to remove it after 'duration' ms. Then animate its entry with requestAnimationFrame — slide in from the bottom over 300ms.
Timers & Animation Loop
mediumThe coffee shop menu needs a countdown timer for flash sales, a toast notification that disappears after 3 seconds, and a smooth animated banner. Each needs a different timer approach.
Timers & Animation Loop
JavaScript is single-threaded — it can only do one thing at a time. Timers schedule callbacks for later without blocking the thread. requestAnimationFrame syncs your animations with the display refresh rate.
setTimeout & setInterval
Run code once after a delay, or repeatedly at an interval:
// Use the buttons to fire timers
// setTimeout — run once after delay: const id = setTimeout(() => doSomething(), 2000) clearTimeout(id) // cancel before it fires // setInterval — run repeatedly: const id = setInterval(() => tick(), 1000) clearInterval(id) // always store the id to stop it
Countdown Timer
A real countdown built with setInterval — with pause, resume, and reset:
let count = 10
const id = setInterval(() => {
count--
display.textContent = count
if (count <= 0) {
clearInterval(id)
showSuccess()
}
}, 1000)requestAnimationFrame — Smooth Animation Loop
The browser calls your function before each repaint — typically 60 times per second:
requestAnimationFramesetInterval(fn, 16)function animate(timestamp) {
const elapsed = timestamp - startTime
// Update position based on elapsed time:
el.style.transform =
`translateX(${Math.sin(elapsed/1000) * 100}px)`
requestAnimationFrame(animate) // loop
}
// Start:
let startTime
requestAnimationFrame(ts => {
startTime = ts
requestAnimationFrame(animate)
})
// Stop:
cancelAnimationFrame(rafId)Your Challenge
Build showToast(msg, ms) — creates a div, appends to body, removes after ms using setTimeout. Then add a CSS entry animation. Use requestAnimationFrame to animate the width of a progress bar from 0 to 100% over exactly 2 seconds using timestamp math.
Challenge
Build a toast notification system: showToast(message, duration) creates a .toast element, appends it to the body, and uses setTimeout to remove it after 'duration' ms. Then animate its entry with requestAnimationFrame — slide in from the bottom over 300ms.