In this lesson, Dev’s laundry is done, but one reminder isn’t enough to bring Dev down from upstairs. You turned the reminder into a nag that repeats every second until Dev collects the laundry.
Key Points
- Like
setTimeout(),setInterval()takes a callback and a delay in milliseconds. A timeout calls its callback once. An interval calls it again and again, every time the delay passes. - An interval never stops on its own. As long as one is active, Node keeps your program running, and you press Ctrl+C to end it.
setInterval()returns a handle. Pass that handle toclearInterval()to stop the interval. Once it’s cleared, no more calls come.- Node counts each delay of an interval from when the last call started, not from when it ended. A slow callback uses part of that delay: with a 600-millisecond check on a 1000-millisecond interval, only 0.4 seconds of rest is left between calls. That rest is the time your program is free for other work.
- A chained timeout, a function that schedules itself again with
setTimeout()at the end of its own run, waits at least the full delay after each call ends, however long the call took. - Each new timer has a new handle, and the function stores each one in the same variable, so that variable is declared with
let, notconst. The function only runs later, after the script has finished, so by then the variable exists. - An interval counts from each start, and a chained timeout counts from each end. Neither is exact: as Kofi’s late reminder showed in Schedule and Cancel a Reminder, a timer that’s due waits while JavaScript is busy. Later in this course, you’ll see why.
The Final Code
The nag, with a timeout that plays the part of Dev coming down for the laundry after three and a half seconds:
nag.mjs
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
const nag = setInterval(() => {
console.log(`${seconds()}s Dev, your laundry is done`);
}, 1000);
setTimeout(() => {
clearInterval(nag);
console.log(`${seconds()}s Dev collected the laundry`);
}, 3500);
Run it with Node, and it prints:
$ node nag.mjs
1.0s Dev, your laundry is done
2.0s Dev, your laundry is done
3.0s Dev, your laundry is done
3.5s Dev collected the laundry
Three nags print, at one, two and three seconds. The interval is cleared at three and a half seconds, so the nag due at four seconds never comes.
The chained version, where a busy loop stands in for a slow check of every machine:
chained_nag.mjs
const start = Date.now();
const seconds = () => ((Date.now() - start) / 1000).toFixed(1);
const checkMachines = () => {
const end = Date.now() + 600;
while (Date.now() < end) {}
};
const remind = () => {
console.log(`${seconds()}s nag starts`);
checkMachines();
console.log(`${seconds()}s nag ends`);
nag = setTimeout(remind, 1000);
};
let nag = setTimeout(remind, 1000);
setTimeout(() => clearTimeout(nag), 4500);
Each nag ends, and only then does a full second of rest begin:
$ node chained_nag.mjs
1.0s nag starts
1.6s nag ends
2.6s nag starts
3.2s nag ends
4.2s nag starts
4.8s nag ends
Next, in Debounce a Chatty Sensor, the dryer gets a door sensor that reports every jiggle, and a timer that waits until the door goes quiet.