📜  描述 Node.js 中 Timer 方法的使用

📅  最后修改于: 2022-05-13 01:56:22.411000             🧑  作者: Mango

描述 Node.js 中 Timer 方法的使用

在本文中,我们将探索 Node.js 中的计时器以及如何在各种场景中使用它们。 node.js 中的 timers 方法包含不同类型的函数,用于在特定时间段执行代码块或函数。它是一个不需要导入的全局模块。

我们可以将定时器模块分为两类函数

  1. 调度定时器:这些类型的定时器用于在指定时间段后调用函数。
  2. 取消定时器:这些类型的定时器用于取消预定的定时器。

1. setImmeditate() 方法:这是一个调度定时器,用于调度回调的立即执行。这些回调函数按预定时间排队,然后执行。此事件循环迭代根据整个回调队列进行处理。当任何即时计时器从正在执行的回调中排队时,计时器将不会被触发。这仅在下一次事件循环迭代之前。

Javascript
// Initializing multiple setImmediate() timer
setImmediate(function A() {
  setImmediate(function B() {
    console.log(1);
    setImmediate(function D() {
      console.log(2);
    });
  });
 
  // Initializing other setImmediate timers
  setImmediate(function C() {
    console.log(3);
    setImmediate(function E() {
      console.log(4);
    });
  });
});
 
console.log('Started Timer Execution');


Javascript
// Method will be executed 1000ms after start
setInterval(function intervalTimer() {
  return console.log('Hey Geek! Welcome to Tutorials Point')
}, 1000);
 
// Executed when the program starts
console.log('This is executed before intervalTimer() method.');


Javascript
// Method will be executed 3 seconds
// after the program starts
setTimeout(function setTimeout() {
  return console.log('Hello Geek! Welcome to GFG');
}, 3000);
 
// Statement will be printed as soon as
console.log('Executed before setTimeout...');


Javascript
// This method will clear the setImmediate() timer
var timer = setImmediate(function A() {
  console.log("Timer is set");
});
 
// clearing the timer
clearImmediate(timer);
console.log("Timer is cleared");


Javascript
// This method will clear the setInterval() timer
// Setting the Interval timer
var timer = setInterval(function A() {
  return console.log("Hello Geek!");
}, 500);
 
// Clearing the interval timer
setTimeout(function() {
  clearInterval(timer);
  console.log("Interval Timer cleared")
}, 2000);


Javascript
// This method will clear the setTimeout() timer
// Setting the Timeout timer
var si1 = setTimeout(function A() {
  return console.log("Hello World!");
}, 3000);
 
// Only the below method is executed
var si2 = setTimeout(function B() {
  return console.log("Hello Geeks!");
}, 3000);
 
// Clearing timer 1
clearTimeout(si1);


输出:

Started...
1
3
2
4

2. setInterval() 方法:该方法重复执行每 t 毫秒后调用的回调。

Javascript

// Method will be executed 1000ms after start
setInterval(function intervalTimer() {
  return console.log('Hey Geek! Welcome to Tutorials Point')
}, 1000);
 
// Executed when the program starts
console.log('This is executed before intervalTimer() method.');

输出:

This is executed before intervalTimer() method.
Hey Geek! Welcome to Tutorials Point
Hey Geek! Welcome to Tutorials Point
Hey Geek! Welcome to Tutorials Point
...

3. setTimeout() 方法:该方法用于调度在一定时间后调用的回调的执行,以毫秒为单位。此超时作为方法中的参数传递。

Javascript

// Method will be executed 3 seconds
// after the program starts
setTimeout(function setTimeout() {
  return console.log('Hello Geek! Welcome to GFG');
}, 3000);
 
// Statement will be printed as soon as
console.log('Executed before setTimeout...');

输出:

Executed before A...
Hello Geek! Welcome to GFG

4. clearImmediate() 方法:该方法用于简单地取消使用 setImmediate() 调度方法创建的调度定时器。

Javascript

// This method will clear the setImmediate() timer
var timer = setImmediate(function A() {
  console.log("Timer is set");
});
 
// clearing the timer
clearImmediate(timer);
console.log("Timer is cleared");

输出:

Timer is cleared

5. clearInterval() 方法:该方法用于取消使用 setInterval() 方法安排的定时器方法。

Javascript

// This method will clear the setInterval() timer
// Setting the Interval timer
var timer = setInterval(function A() {
  return console.log("Hello Geek!");
}, 500);
 
// Clearing the interval timer
setTimeout(function() {
  clearInterval(timer);
  console.log("Interval Timer cleared")
}, 2000);

输出:

Hello Geek!
Hello Geek!
Hello Geek!
Interval Timer cleared

6. clearTimeout() 方法:该方法用于清除使用 setTimeout() 方法设置的计划定时器。

Javascript

// This method will clear the setTimeout() timer
// Setting the Timeout timer
var si1 = setTimeout(function A() {
  return console.log("Hello World!");
}, 3000);
 
// Only the below method is executed
var si2 = setTimeout(function B() {
  return console.log("Hello Geeks!");
}, 3000);
 
// Clearing timer 1
clearTimeout(si1);

输出:

Hello Geeks!