Node.js – 定時器模組 – 定時排程


**定時器**模組包含可在一段時間後執行程式碼的函式。您無需顯式匯入**定時器**模組,因為它在模擬的瀏覽器 JavaScript API 中已全域性可用。

**定時器**模組主要分為兩類

  • **定時排程** – 此定時器將任務安排在某個時間點後執行。

    • setImmediate()

    • setInterval()

    • setTimeout()

  • **取消定時器** – 這種型別的定時器取消了已設定要執行的計劃任務。  

    • clearImmediate()

    • clearInterval()

    • clearTimeout()

定時排程

1. setTimeout() 方法

**setTimeout()** 方法在指定毫秒數後安排程式碼執行。只有在超時發生後,才會執行程式碼。指定的函式將僅執行一次。此方法返回一個 ID,可用於 **clearTimeout()** 方法。

語法

setTimeout(function, delay, [args])

示例 1

let str = 'TutorialsPoint!';

setTimeout(function () {
   // Will be printed after 2 seconds
   return console.log(str);
}, 2000);

// This will be printed immediately
console.log('Executing setTimeout() method');

輸出

Executing setTimeout() method
TutorialsPoint!

2. setImmediate() 方法

**setImmediate()** 在當前事件迴圈週期結束時執行程式碼。

語法

setImmediate(function, [args])

示例

檔名 - immediate.js

// Setting timeout for the function
setTimeout(function () {
   console.log('setTimeout() function running');
}, 5000);

// Running this function immediately before any other
setImmediate(function () {
   console.log('setImmediate() function running');
});
// Directly printing the statement
console.log('Simple statement in the event loop');

輸出

Simple statement in the event loop
setImmediate() function running
setTimeout() function running

3. setInterval() 方法

**setInterval()** 在指定間隔後執行程式碼。在間隔過去後,該函式將多次執行。該函式將一直呼叫,直到外部停止程序或在指定時間段後使用程式碼停止。

語法

setInterval(function, delay, [args])

示例

檔名 - interval.js

setInterval(function() {
   console.log('Tutoirals Point - SIMPLY LEARNING !');
}, 1000);

輸出

Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !
Tutoirals Point - SIMPLY LEARNING !

4. clearImmediate() 方法

此方法清除由 **setImmediate()** 方法建立的立即定時器物件。

語法

clearImmediate(timer)

示例

檔名 - clearImmediate.js

// clearImmediate() Example

var timer = setImmediate(function A() {
   console.log("Timer set");
});

clearImmediate(timer);
console.log("Timer cancelled");

輸出

Timer cancelled

5. clearInterval() 方法

此方法清除由 **setInterval()** 方法建立的立即定時器物件。

語法

clearInterval(timer)

示例

檔名 - clearInterval.js

// clearInterval() Example

var si = setInterval(function A() {
   return console.log("Setting Intevals for 500 ms !");
}, 500);

// Cleared the interval from 1000 ms
setTimeout(function() {
   clearInterval(si);
}, 1000);

輸出

Setting Intevals for 500 ms !

6. clearTimeout() 方法

此方法清除由 **setTimeout()** 方法建立的立即定時器物件。

語法

clearTimeout(timerObject)

示例

檔名 - clearTimeout.js

// clearTimeout() Example

var timer = setTimeout(function A() {
   return console.log("Hello TutorialsPoint!");
}, 500);

// timer2 will be executed
var timer2 = setTimeout(function B() {
   return console.log("Welcome to TutorialsPoint!");
}, 500);
clearTimeout(timer);

輸出

Welcome to TutorialsPoint!

更新於: 2021-10-27

423 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告