0
0
Node.jsframework~10 mins

setInterval and clearInterval in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run a function every 2 seconds using setInterval.

Node.js
const intervalId = setInterval([1], 2000);
Drag options to blanks, or click blank then click option'
A() => console.log('Hello')
Bconsole.log('Hello')
Cconsole.log
DsetTimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Passing console.log('Hello') directly runs it once instead of repeatedly.
Passing setTimeout instead of setInterval.
2fill in blank
medium

Complete the code to stop the interval after 5 seconds using clearInterval.

Node.js
const intervalId = setInterval(() => console.log('Tick'), 1000);
setTimeout(() => [1](intervalId), 5000);
Drag options to blanks, or click blank then click option'
AstopInterval
BsetInterval
CclearTimeout
DclearInterval
Attempts:
3 left
💡 Hint
Common Mistakes
Using clearTimeout instead of clearInterval.
Trying to call setInterval again to stop it.
3fill in blank
hard

Fix the error in the code to correctly stop the interval after 3 seconds.

Node.js
const id = setInterval(() => console.log('Running'), 1000);
setTimeout(() => clearInterval([1]), 3000);
Drag options to blanks, or click blank then click option'
AintervalId
Bid
CsetInterval
DclearInterval
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a wrong variable name.
Using clearTimeout instead of clearInterval.
4fill in blank
hard

Fill both blanks to create an interval that logs 'Hi' every second and stops after 4 seconds.

Node.js
const [1] = setInterval(() => console.log('Hi'), 1000);
setTimeout(() => [2]([1]), 4000);
Drag options to blanks, or click blank then click option'
AintervalId
BclearInterval
CsetInterval
DtimeoutId
Attempts:
3 left
💡 Hint
Common Mistakes
Using setInterval instead of clearInterval to stop.
Using a variable name that doesn't match.
5fill in blank
hard

Fill all three blanks to create an interval that counts seconds and stops after 3 counts.

Node.js
let count = 0;
const [1] = setInterval(() => {
  [2];
  console.log(count);
  if (count === 3) {
    [3]([1]);
  }
}, 1000);
Drag options to blanks, or click blank then click option'
AintervalId
B++count
CclearInterval
Dcount++
Attempts:
3 left
💡 Hint
Common Mistakes
Using pre-increment (++count) instead of post-increment (count++).
Using clearTimeout instead of clearInterval.