← Back CP ๐Ÿงช Potion Timer Lab
๐Ÿฐ Magic Castle โ€” In Star Catcher your code waited for a TAP to react. Today your code runs ALL BY ITSELF on a timer! Potions need to brew for exactly the right amount of time, and cauldrons need stirring on a steady beat. Meet setTimeout โ€” "do this once after a delay" โ€” and setInterval โ€” "do this again and again on schedule." No taps needed. Code that ticks like a clock!

โฐ Timers โ€” Code That Runs on Its Own

A timer is code that fires automatically on a schedule. setTimeout waits a certain number of milliseconds, then runs once โ€” like a potion timer that dings when the brew is ready. setInterval runs the same code over and over every N milliseconds โ€” like a cauldron stirring itself every few seconds. And clearInterval stops the loop when you're done. Every clock, countdown, and auto-refresh in the world uses these!

// ๐Ÿงช TIMER 1 โ€” setTimeout: runs ONCE after a delay
setTimeout(function() {
  potion.color = 'purple';  // ๐ŸŽจ happens after N ms!
  done = true;  // โœ… brew complete!
}, 2000);  // โณ wait 2000ms, then FIRE

// ๐Ÿ” TIMER 2 โ€” setInterval: runs REPEATEDLY
var loop = setInterval(function() {
  stir();  // ๐ŸŒ€ stir! fires every N ms
  count++;
  if (count >= 4) clearInterval(loop);  // ๐Ÿ›‘ stop!
}, 800);  // โฑ every 800ms
๐ŸŽฏ Missions Brew 3 potions! Make a potion sparkle! Finish all 5 challenges!
๐Ÿงช potion-timer.js — Your Timer Engine
1// ๐Ÿงช BREW STATE โ€” what the cauldron remembers
2var color = 'purple', stirs = 0, done = false;
3
4// โฐ setTimeout โ€” runs ONCE after a delay (auto, no tap!)
5setTimeout(function() {
6  potion.color = 'purple';
7  done = true;  // โœ… brew complete!
8}, 2000);  // โณ wait N ms
9
10// ๐Ÿ” setInterval โ€” repeats on a schedule
11var stirLoop = setInterval(function() {
12  stir();  // ๐ŸŒ€ stir the cauldron!
13  stirs++;  // count each stir
14  if (stirs >= 4) clearInterval(stirLoop);  // ๐Ÿ›‘ stop!
15}, 800);  // โฑ every N ms
๐Ÿงช Potion Cauldron โ€” set your timer rules, then brew!
โฑ Status: Set your timer rules, then tap BREW!

๐ŸŽ›๏ธ Set Your Timer Rules

โฐ Brew Delay (setTimeout) โ€” how long until potion is done
Delay
2000 ms
๐Ÿ” Stir Interval (setInterval) โ€” how often the cauldron stirs
Interval
800 ms
๐Ÿ”ข Stir Count โ€” how many stirs before stopping
Stirs
4
๐ŸŽจ Potion Color โ€” choose your brew's color
Color
โœจ Sparkle Effect โ€” potion sparkles when done
Sparkle

๐Ÿงช Potion Timer Challenges

1
โฐ First Brew: Press START BREWING and watch the cauldron โ€” after your Brew Delay, the potion completes by itself! That's setTimeout firing on a schedule, no tap needed!
setTimeout
โœ…
2
๐Ÿ” Stir Cycle: Watch the cauldron stir itself on repeat โ€” each stir is a setInterval tick firing automatically. See the bubbles and stir count go up!
setInterval
โœ…
3
โšก Speed Interval: Set Stir Interval to 200 ms (fastest) and brew โ€” watch the cauldron stir super-fast! The timer loop fires more often because the interval is shorter.
frequency
โœ…
4
๐ŸŽจ Color Change: Pick a different Potion Color (Red, Green, Blue, or Gold) and brew โ€” the setTimeout applies your color choice to the potion when it completes!
variable
โœ…
5
โœจ Sparkle Master: Turn Sparkle ON and brew โ€” when the potion finishes, sparkles burst out! Then try with Sparkle OFF to see the difference.
effect
โœ…
๐Ÿฐ Magic Castle Progress0%

๐Ÿงช Welcome to Potion Timer Lab!

You built a game that reacts to taps in Star Catcher. Now learn code that runs all by itself on a TIMER! Potions don't brew instantly โ€” they need time, and cauldrons need stirring on a beat. That's exactly what setTimeout (wait once) and setInterval (repeat forever) do!

โฐSet Brew Delay โ€” how many ms until your potion is done (setTimeout)
๐Ÿ”Set Stir Interval โ€” how often the cauldron stirs itself (setInterval)
๐ŸŽจChoose a Potion Color โ€” the setTimeout applies it when done!
๐ŸงชPress START BREWING and watch both timers fire automatically โ€” no taps needed!
๐Ÿ†Finish all 5 challenges to become a Timer Master!