Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Node.js | web server creation

Can someone explain me this part of code? What's \n? Is this a loop? (can't recognize the syntax)

setInterval(function(){
    response.write(i + "\n");
    if(i === 0) {
      response.end("Blast off\n");
    }
    i--;
  }, 1000);

1 Answer

Well, it's a countdown timer, something else must be calling this, and somehow setting i. But let's assume i = 5. setInterval is a javascript function that does something at an interval, in this case, every 1000 milliseconds, or 1 second this runs.

\n is just a new line character, for human friendly readability

So if i was 5, it out put this

5

4

3

2

1

0

Blast off

Over a 6 second span of time, it would countdown every second. Basically, and we write i, we're subtracting 1 from i. Eventually, i = 0, is when it displays Blast off.