Node.js โมดูล ตัว จับเวลา

❮ โมดูลในตัว


ตัวอย่าง

เขียน "สวัสดี" ทุก ๆ 500 มิลลิวินาที:

var myInt = setInterval(function () {
    console.log("Hello");
}, 500);

ความหมายและการใช้งาน

โมดูลตัวจับเวลาให้วิธีการเรียกใช้ฟังก์ชันการตั้งเวลาในภายหลังในเวลาที่กำหนด

ออบเจ็กต์ Timer เป็นอ็อบเจ็กต์ส่วนกลางใน Node.js และไม่จำเป็นต้องนำเข้าโดยใช้requireคีย์เวิร์ด


วิธีการจับเวลา

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.

❮ โมดูลในตัว