Global objects in Node.js
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.
For 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.
For 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) is used to 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.
For 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) is used to stop a Timer created by the previous setTimeout () function. The parameter t is the Timer returned from the setTimeout () function.
For 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) is used to 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.
For 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:
Console object : Used to print information on stdout and stderr.
Process object : Used to get information about the current process.
According to Tutorialspoint
Previous post: Stream in Node.js
Next article: Utility Module in Node.js
You should read it
May be interested
- Warning: Global Internet may be interrupted in the next 48 hoursthe internet and digital internet corporation (icann) will make changes to the security keys for dns (domain names) for maintenance and this may cause the global internet to have connectivity problems in about 48 coming.
- Sort and group objects in PowerPoint 2016users can arrange objects on the slide at will by aligning, ordering, grouping and rotating them in different ways.
- Ragnarok Origin Global on PCdownload ragnarok origin now and have a wonderful experience in a fantasy world with the gods of norse mythology.
- How to use Trackr to find objectsperhaps everyone at least once forgot their wallet, car keys, phone and many other things that they couldn't remember where they left them. with bluetooth trackr tracking device, you can find them easily.
- Steps to count objects in AutoCADcounting objects in autocad. you are curious to know how many lines, blocks, layers... in the following article, let's learn about simple and quick tips for counting objects in cad.
- Basic operations on objects on slides in PowerPointintroduce the basic operations with objects on slide in powerpoint 2013. the example on silde has 2 objects as shown: the operations for working with 2 objects on silde include: 1. moving the object to 1 unit new position. 2. copy the object to 1 page
- Complete Word 2016 tutorial (Part 20): Align, align, and group objectswhen using word, sometimes you need to use multiple objects such as images, shapes, text box, graphics. you can organize these objects by aligning, grouping, organizing and rotating them in different ways. this will make your document look more creative, beautiful and special. please follow the article below for details!
- Need to be wary of foreign objects in the earforeign objects or insects flying into the ear can cause more harm than you think
- Instructions for deleting objects in photos on iOS 18the iphone 15 series and above, after upgrading to ios 18, have been equipped with new apple intelligence technology, capable of deleting objects in photos through the clean up tool.
- Tomorrow, global GPS will end its operation cycle, 'Y2K incident' can happen againat the end of this week, the global gps system will end its operation cycle. this event made many people remember the y2k error that happened in 2000.