Timers

Question Click to View Answer

Describe how the following code functions.

setTimeout(function() {
  console.log('greetings');
}, 1000);

This code waits 1000 milliseconds (1 second) and then prints 'greetings' to the console.

The setTimeout function takes a callback function as the first argument and an integer that represents time in milliseconds as the second argument. The callback function is invoked after the time elapses.

Describe how the following code functions.

setInterval(function() {
  console.log('boo!');
}, 500);

This code will print "boo!" to the console every 500 milliseconds (every half second).

What does the following code print to the console?

const smelly = setTimeout(function() {
  console.log("throw stink bomb");
}, 1000);
clearTimeout(smelly);

The smelly constant would normally cause the "throw stink bomb" string to be printed to the console after a second. However, the clearTimeout function removes the timer before it has a chance to execute. Nothing is printed to the console.

What does the following code print to the console?

const hola = setInterval(function () {
  console.log("hola!");
}, 500);

setTimeout(function () {
  clearInterval(hola);
}, 2600);

This code prints "hola!" to the console five times. "hola" is printed at 500 milliseconds, 1000 milliseconds, 1500 milliseconds, 2000 milliseconds, and 2500 milliseconds. After 2600 milliseconds, the timer is cleared and "hola!" isn't printed anymore.

What does the following code print to the console?

setTimeout(function() {
  console.log("before");
}, 1000);

setTimeout(function() {
  console.log("middle");
}, 0);

console.log("after");
after
middle
before

The setTimeout(callback, 0) pattern is used to execute an action after all pending actions are executed, so "middle" isn't printed until "after" is printed. "before" isn't printed until a second has passed.

What does the following code print to the console?

setTimeout(function() {
  console.log("before");
}, 1000);

process.nextTick(function() {
  console.log("middle");
});

console.log("after");
after
middle
before

process is a global object that's defined by Node. Node executes code based on an event loop. The loop is an ordered list of commands that are run sequentially. Each event that is run in the Node event loop is referred to as a "tick".

The process.nextTick() function executes the callback when all the events in the queue have been run. console.log("after"); is added to the queue first and console.log("middle"); is executed when the first tick completes.

What does the following code print to the console?

process.nextTick(function first_cool() {
  console.log("snow day");
  let counter = 0;
  while(true) {
    counter = counter + 1;
  }
});

process.nextTick(function second_cool() {
  console.log("rain day");
});

"snow day" is printed and nothing else is printed because the code gets stuck in an infinite loop.

The Node event loop is single threaded and blocking. Subsequent events aren't executed until previous events are done running. first_cool runs forever, so second_cool never executes.

Make sure you have a good command of how the Node event loop functions - it's an important concept to master.