Is there a timer in node.js?

WBOY
Release: 2022-07-04 17:28:51
Original
1723 people have browsed it

There is a timer in "node.js"; the timer module in "node.js" contains a function that executes the code once for a certain period of time. The timer does not need to be introduced through require(), because all The methods all simulate JavaScript functions in the browser and are global; the timer function in "node.js" implements an API similar to the timer API provided by the web browser.

Is there a timer in node.js?

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

There is a timer in node.js

The timer in Node.js

The timer module in Node.js contains a function that executes the code at a certain interval . The timer does not need to be introduced through require(), because all these methods simulate JavaScript functions in the browser and are global. To get a better overall understanding of when these functions will be executed, it's a good idea to read Event Polling in Node.js.

Controlling time continuity with Node.js

The API functions in Node.js provide several ways to allow code to start executing at a certain moment after the current time. The functions given below look similar as they are available in most browsers. But Node.js actually provides its own implementation. Timers are very tightly integrated into the system, and although these APIs are copies of the functions in the browser, they are still implemented differently.

timer is used to schedule a function to be called at a certain point in the future. The timer function in Node.js implements an API similar to the timer API provided by the web browser, but uses an event loop implementation. There are four related methods in Node.js

setTimeout(callback, delay[, ...args])
setInterval(callback[, ...args])
setImmediate(callback[, ...args])
process.nextTick(callback[, ...args])
Copy after login

Examples are as follows:

setTimeout() can be used to execute a certain function after a specified period of time. Code tasks. This function is very similar to the browser JavaScript function window.setTimeout(), but you cannot pass a string into execution.

setTimeout() accepts an executable function as its first parameter, and then has a delay time in milliseconds as the second parameter. The remaining parameters can also be included as parameters passed to this function. The following is an example:

function myFunc(arg) {
  console.log(`arg was => ${arg}`);
}
setTimeout(myFunc, 1500, 'funky');
Copy after login

Because setTimeout() is used, the above function myFunc() will be executed around 1500 milliseconds (or 1.5 seconds).

The set timing interval cannot guarantee that the code will be executed at a precise millisecond interval every time. This is because other code that is blocked or is being processed on event polling will delay the execution of this timing. The only guarantee is that the timer will not execute earlier than the declared interval.

setTimeout() returns a Timeout object through which the set timer can be referenced. The returned object can be used to cancel timing (see clearTimeout() below) and to change execution behavior (see unref() below).

"Execute immediately after" setImmediate()

setImmediate() will be executed at the end of the current event poll. This code will execute after any I/O operations in the current event poll and before any next timer cycle. Code execution can be thought of as "executing immediately after", meaning that any immediately following setImmediate() function call will execute before the setImmediate() function parameters.

The first parameter of setImmediate() is the function to be executed. When executed, the subsequent parameters will be passed into this function as parameters. Here is an example:

console.log('before immediate');
setImmediate((arg) => {
  console.log(`executing immediate: ${arg}`);
}, 'so immediate');
console.log('after immediate');
Copy after login

The above function passed into setImmediate() will be executed after any executable code has been executed, so the output is:

before immediate
after immediate
executing immediate: so immediate
Copy after login

setImmediate() returns an Immediate Object, which can be used to cancel scheduled scheduled tasks (see clearImmediate() below).

Note: Do not confuse setImmediate() with process.nextTick(). They have some major differences: First, process.nextTick() will execute before any Immediate set and before any scheduled I/O. Second, process.nextTick() is not erasable. In other words, once code is executed using process.nextTick(), the execution cannot be interrupted. It is just like an ordinary function. For details, you can refer to this tutorial to learn more about it. Understand the operation of process.nextTick().

"Forever polling" execution ~ setInterval()

If there is a function that needs to be executed multiple times, setInterval() can come in handy. setInterval() accepts a function as its parameter, which will be run infinite times. The second parameter is a given delay in milliseconds. Just like setTimeout(), the remaining parameters can be added after this and used as arguments passed to the function call. Another point similar to setTimeout() is that the delay is not guaranteed to be precise, because some operations may hang on the event polling, so it can be considered an approximate delay. As in the following example:

function intervalFunc() {
  console.log('Cant stop me now!');
}
setInterval(intervalFunc, 1500);
Copy after login

In the above example, intervalFunc() is executed every 1500 milliseconds (or every 1.5 seconds) until it is terminated.

Like setTimeout(), setInterval() also returns a Timeout object, which can be referenced and change the set timer.

Recommended learning: "nodejs video tutorial"

The above is the detailed content of Is there a timer in node.js?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!