Bootcamp
Search…
📅
Course Schedule
0: Language and Tooling Teaching Guide
1: Frontend Basics Teaching Guide
2: Backend Basics Teaching Guide
3: Backend Applications Teaching Guide
4: Backend Structure Teaching Guide
5: Full-Stack Applications Teaching Guide
6: Frontend Infrastructure Teaching Guide
7: React Teaching Guide
8: Advanced React Teaching Guide
9: Advanced Topics Teaching Guide
🧮
Algorithms Teaching Guide
💼
Interview Prep Teaching Guide
☺
User Experience Teaching Guide
1.7: setInterval

Introduction

setInterval repeats a callback function indefinitely at a designated interval until stopped.

Infinite Example

1
console.log('starting...');
2
​
3
const delayInMilliseconds = 1000; // this is one second
4
​
5
const ref = setInterval(() => {
6
console.log(`I happen after ${delayInMilliseconds}`);
7
}, delayInMilliseconds);
8
​
9
console.log('bananas!');
Copied!

Example that Stops

1
console.log('starting...');
2
​
3
const delayInMilliseconds = 1000; // this is one second
4
let counter = 0;
5
​
6
const ref = setInterval(() => {
7
console.log(`I happen after ${delayInMilliseconds}`);
8
console.log(counter);
9
counter += 1;
10
​
11
if (counter > 10) {
12
clearInterval(ref);
13
}
14
}, delayInMilliseconds);
15
​
16
console.log('bananas!');
Copied!

Exercise

Run the above code.
Last modified 2mo ago