Node.js -Timer- Modul
Beispiel
Schreiben Sie alle 500 Millisekunden „Hallo“:
var myInt = setInterval(function () {
console.log("Hello");
}, 500);
Definition und Verwendung
Das Timers-Modul bietet eine Möglichkeit, Funktionen zu planen, die später zu einem bestimmten Zeitpunkt aufgerufen werden.
Das Timer-Objekt ist ein globales Objekt in Node.js und muss nicht mit dem require
Schlüsselwort importiert werden.
Timer-Methoden
Method | Description |
---|---|
clearImmediate() | Cancels an Immediate object |
clearInterval() | Cancels an Interval object |
clearTimeout() | Cancels a Timeout object |
ref() | Makes the Timeout object active. Will only have an effect if the Timeout.unref() method has been called to make the Timeout object inactive. |
setImmediate() | Executes a given function immediately. |
setInterval() | Executes a given function at every given milliseconds |
setTimeout() | Executes a given function after a given time (in milliseconds) |
unref() | Stops the Timeout object from remaining active. |