Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use global functions in node

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "how to use the global function in node", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the global function in node" can help you solve your doubts.

Global function: 1, setTimeout () function, used to execute the specified function after the specified millisecond; 2, clearTimeout () function, used to stop the timer created by setTimeout (); 3, setInterval (cb, ms) function, used to set the timer and return a handle value.

This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.

Global function in node

1. Timer function:

There are four, namely: setTimeout (), clearTimeout (), setInterval (), clearInterval ().

SetTimeout (cb, ms)

The setTimeout (cb, ms) global function executes the specified function (cb) after the specified number of milliseconds (ms). : setTimeout () executes the specified function only once. Returns a handle value that represents a timer.

Example

Create the file main.js with the following code:

Function printHello () {console.log ("Hello, World!");} / / execute the above function setTimeout (printHello, 2000) in two seconds

Execute the main.js file with the following code:

$node main.jsHello, WorldClearTimeout (t)

The clearTimeout (t) global function is used to stop a timer previously created by setTimeout (). The parameter t is a timer created by the setTimeout () function.

Example

Create the file main.js with the following code:

Function printHello () {console.log ("Hello, World!");} / / execute the above function var t = setTimeout (printHello, 2000) after two seconds; / / clear timer clearTimeout (t)

Execute the main.js file with the following code:

$node main.jssetInterval (cb, ms)

The setInterval (cb, ms) global function executes the specified function (cb) after the specified number of milliseconds (ms). Returns a handle value that represents a timer. You can use the clearInterval (t) function to clear the timer. The setInterval () method keeps calling the function until clearInterval () is called or the window is closed.

Example

Create the file main.js with the following code:

Function printHello () {console.log ("Hello, World!");} / / execute the above function setInterval (printHello, 2000) in two seconds

Execute the main.js file with the following code:

$node main.js

The above program will output "Hello, World!" every two seconds and will be executed permanently until you press the ctrl + c button.

2. Require function: used to load the module.

The parameter is the module file name of the full path, or directly the module name

Require.main: used to detect whether a module is the main module in the application (written inside the detected module file)

If (module = require.main) {console.log ('true')}

Multiple references to the same module do not cause multiple execution of code within the module

Require.resolve: query the file name of a module file with the full absolute path. However, the module will not be loaded

Require.resolve ('. / testModule.js')

Require.cache object: represents the cache area where all loaded modules are cached

Console.log (require.cache)

You can access a module by contending for a name

Require.cache ['module file name'] read here, this article "how to use global functions in node" has been introduced, you still need to practice and use the knowledge points of this article before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report