Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

The Settimeout Function (Cb: Global Objects in Node.js

Understand The Settimeout Function (Cb: Global Objects in Node.js with clear explanations, practical examples, and useful tips. This updated guide covers...

Table of Contents

The Settimeout Function (Cb: Global Objects in Node.js is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

The Global Concept (Global) means everything has access to it. In Node.js, too, global objects are available for all modules. We do not need to declare or import them by the require () method but can still use them directly. These objects can be Modules, functions, strings or objects.

__filename in Node.js

In Node.js, __f ilename represents the file name of the code being executed. This is the absolute path to the file containing this code.

As an example,

To illustrate _filenam in Node.js, create main.js with the following simple content:

 // Print list of __filename in Node.js console . log ( __filename ); 

Run main.js to see the result:

 $ node main . js 

The result of printing is the path to main.js containing the above code:

 /web/com/1427091028_21099/main.js 

__dirname in Node.js

In Node.js, __dirname represents the directory containing the code that is being executed.

As an example,

To illustrate getting the information of _dirname in Node.js, create main.js containing the following simple code:

 // Print list of __dirname in Node.js console . log ( __dirname ); 

Run main.js to see the result::

 $ node main . js 

The result is the current directory containing main.js:

 / web / com / 1427091028_21099 

SetTimeout function (cb, ms) in Node.js

The global setTimeout function (cb, ms) helps run a callback function named cb after a milisecond interval.

This global function returns a value representing the Timer so that it can be deleted by the clearTimeout () function.

As an example,

To illustrate the usage of the setTimeout () function in Node.js, create main.js with the following content:

 function printHello () { console . log ( "Hello World!" ); } // Fly to printHello after 2 hours of setTimeout ( printHello , 2000 ); 

Run main.js to see the result::

 $ node main . js 

The result will appear after about 2s. This time period may be longer than 2s because the delay time is also affected by external factors such as OS Timer:

 Hello World! 

The clearTimeout (t) function in Node.js

The clearTimeout global function (t) helps stop a Timer created by the previous setTimeout () function. The parameter t is the Timer returned from the setTimeout () function.

As an example,

To delete the previously set Timer, create main.js with the following simple content. First, we set the Timer by the setTimeout () function, then use the clearTimeout () function to delete this value:

 function printHello () { console . log ( "Hello World!" ); } // Fly to printHello after 2 hours var t = setTimeout ( printHello , 2000 ); // Fly the skin timer to get the clearTimeout ( t ); 

Run main.js to see the result::

 $ node main . js 

No results will be displayed on the terminal.

The setInterval function (cb, ms) in Node.js

The global function setInterval (cb, ms) helps run the callback function named cb repeatedly after a millisecond ms interval.

This global function returns a value representing the Timer so that it can be deleted by the clearInterval (t) function.

As an example,

To see the usage of the Node.js setInterval () function, create main.js with the following simple content:

 function printHello () { console . log ( "Hello World!" ); } // Fly, we call pringtHello after 2 hours setInterval ( printHello , 2000 ); 

Run main.js to see the result::

 $ node main . js 

The above program will execute the printHello () function every 2 seconds.

Some global objects in Node.js

In addition to the above global properties and functions, Node.js also includes some other global objects. Follow the following link to find out the details:

FAQ

What should you know about __filename in Node.js?

In Node.js, __f ilename represents the file name of the code being executed. This is the absolute path to the file containing this code.

What should you know about __dirname in Node.js?

In Node.js, __dirname represents the directory containing the code that is being executed.

What should you know about setTimeout function (cb, ms) in Node.js?

The global setTimeout function (cb, ms) helps run a callback function named cb after a milisecond interval.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.