Tags

Node.js

Node JS: Basics – Part 2

LOGGING AND TIMERS

Clearing Timers

The other way to do is to use another function that’s available in the global objects. It’s called clearInterval(). This is the right way of doing things. To use the clearInterval() function, we should initiate the setInterval() to another variable this way:

var timer = setInterval(function () { … }, 1000);

Now, the timer will contain an integral value, an identifier for the setInterval() function to send to the clearInterval() function. We can use a simple if condition to check if the time is over ten seconds, we call the clearInterval() function on the identifier timer, that we set earlier. To clear any intervals, the way we need to clear is by this syntax:

var timer = setInterval(function () { … }, 1000);

clearInterval(timer);

It’s so simple. Let’s try the whole code again with the new condition to stop the timer when the time reaches ten or when the application has run ten seconds.

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

$ cat app.js

console.log(“App Started.”);

var time = 0;

var timer = setInterval(function () {

  time ++;

  console.log(“Hey, it’s been ” + time + ” whole seconds!”);

  if (time > 10)

    clearInterval(timer);

}, 1000);

And let’s see how this code runs.

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

$ node app

App Started.

Hey, it’s been  1 whole seconds!

Hey, it’s been  2 whole seconds!

Hey, it’s been  3 whole seconds!

Hey, it’s been  4 whole seconds!

Hey, it’s been  5 whole seconds!

Hey, it’s been  6 whole seconds!

Hey, it’s been  7 whole seconds!

Hey, it’s been  8 whole seconds!

Hey, it’s been  9 whole seconds!

Hey, it’s been 10 whole seconds!

Cool. Now in this case, you could see that the application doesn’t run beyond ten seconds. The main thing here is, we have seen all these functions in the window object, and I wanted to stress the fact that these are also available on the Node JS global object in command line too and are available in the functions. As I say, global objects, they can be used anywhere inside the program, no matter where you trigger them.

File System Functions

There are some more cool functions in the global object. Node JS can tell us where we are with few global functions. This is so cool because, it helps us to debug the errors that occur, when we try to do a large dump. This is similar to the PHP super globals, which start with __. Let’s have a look at __dirname now:

console.log(__dirname);

You can use the above keyword wherever in your code. The above line prints out the current directory, where the app is getting executed.

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

$ cat app.js

console.log(__dirname);

That’s the code I have above and when I execute it, all I get to see is my current working directory.

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

$ node app

C:\Users\praveen\NodeJS

This is super cool, isn’t it? Now there’s one more similar keyword, which tells the file name. The complementary one is getting the file name, and as you would have guessed it, it’s __filename. I am going to use both the functions in this app and show you how it displays.

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

$ cat app.js

console.log(__dirname);

console.log(__filename);

That’s the contents of the app.js and when we run it using our node app, all we get is:

praveen@PRAVEEN.SCIENCE MINGW64 ~/NodeJS

$ node app

C:\Users\praveen\NodeJS

C:\Users\praveen\NodeJS\app.js

That’s pretty cool, isn’t it? This way, you will be able to better debug your applications like getting where the exception was thrown, etc. and get to the exact file in seconds. These are the two really cool file system utilities we can use anywhere in Node JS to get to know the right file that’s getting executed at the moment, because they are present on the global object.

Again, have a look at the Global Objects in Node JS Documentation, where it covers these in more detail. Next week, we’ll have a look on something more important, which is related to injecting new JavaScript files dynamically using require() function. This function also comes under File System functions, as this deals with inclusion of files by accessing the file system, rather than traditionally including them.

Thanks to Praveen Kumar for being our guest writer this week.

Who Are Ronald James?

We are a leading niche digital & tech recruitment specialist for the North East of England. We Specialise in the acquisition of high-performing technology talent across a variety of IT sectors including Digital & Technology Software Development.

Our ultimate goal is to make a positive impact on every client and candidate we serve - from the initial call and introduction, right up to the final delivery, we want our clients and candidates to feel they have had a beneficial and productive experience.

Contact our Team

If you’re looking to start your journey in sourcing talent or find your dream job, you’ll need a passionate, motivated team of experts to guide you. Check out our Jobs page for open vacancies. If interested, contact us or call 0191 300 6501 for a quick chat with our team.

Let's be Friends!

Follow us on our blog, FacebookLinkedInTwitter or Instagram to follow industry news, events, success stories and new blogs releases.

 

Back to Blog

</Follow Us>