Mô-đun hẹn giờ trong nút. js chứa các hàm thực thi mã sau một khoảng thời gian đã đặt. Bộ hẹn giờ không cần nhập qua require[]
, vì tất cả các phương thức đều có sẵn trên toàn cầu để mô phỏng API JavaScript của trình duyệt. Để hiểu đầy đủ khi nào các chức năng hẹn giờ sẽ được thực thi, bạn nên đọc trên Node. vòng lặp sự kiện js
Kiểm soát thời gian liên tục với nút. js
nút. js API cung cấp một số cách lập lịch mã để thực thi tại một thời điểm nào đó sau thời điểm hiện tại. Các chức năng bên dưới có vẻ quen thuộc vì chúng có sẵn trong hầu hết các trình duyệt, nhưng Node. js thực sự cung cấp triển khai riêng của các phương thức này. Bộ hẹn giờ tích hợp rất chặt chẽ với hệ thống và mặc dù thực tế là API phản chiếu API của trình duyệt, vẫn có một số khác biệt trong quá trình triển khai
"Khi tôi nói như vậy" Thi hành ~ setTimeout[]
setTimeout[]
có thể được sử dụng để lên lịch thực thi mã sau một khoảng mili giây được chỉ định. Chức năng này tương tự như window.setTimeout[]
từ API JavaScript của trình duyệt, tuy nhiên một chuỗi mã không thể được chuyển để được thực thi
setTimeout[]
chấp nhận một hàm để thực thi làm đối số đầu tiên của nó và độ trễ mili giây được xác định là một số làm đối số thứ hai. Các đối số bổ sung cũng có thể được đưa vào và chúng sẽ được chuyển đến hàm. Đây là một ví dụ về điều đó
function myFunc[arg] {
console.log[`arg was => ${arg}`];
}
setTimeout[myFunc, 1500, 'funky'];
Hàm trên
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
1 sẽ thực thi trong khoảng thời gian gần 1500 mili giây [hoặc 1. 5 giây] càng tốt do cuộc gọi của setTimeout[]
Khoảng thời gian chờ được đặt không thể dựa vào để thực thi sau số mili giây chính xác đó. Điều này là do mã thực thi khác chặn hoặc giữ vòng lặp sự kiện sẽ đẩy lùi thời gian chờ thực thi. Đảm bảo duy nhất là thời gian chờ sẽ không thực hiện sớm hơn khoảng thời gian chờ đã khai báo
setTimeout[]
trả về một đối tượng
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 có thể được sử dụng để tham chiếu thời gian chờ đã được đặt. Đối tượng được trả về này có thể được sử dụng để hủy bỏ thời gian chờ [xem console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
5 bên dưới] cũng như thay đổi hành vi thực thi [xem console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
6 bên dưới]"Ngay sau đó" Thi hành ~ console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7 sẽ thực thi mã vào cuối chu kỳ vòng lặp sự kiện hiện tại. Mã này sẽ thực thi sau bất kỳ thao tác I/O nào trong vòng lặp sự kiện hiện tại và trước bất kỳ bộ hẹn giờ nào được lên lịch cho vòng lặp sự kiện tiếp theo. Việc thực thi mã này có thể được coi là xảy ra "ngay sau điều này", nghĩa là bất kỳ mã nào sau lời gọi hàm console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7 sẽ được thực thi trước đối số hàm console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7Đối số đầu tiên của
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7 sẽ là hàm thực thi. Mọi đối số tiếp theo sẽ được chuyển đến hàm khi nó được thực thi. Đây là một ví dụconsole.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
Hàm trên được truyền cho
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7 sẽ thực thi sau khi tất cả mã có thể chạy được đã thực thi và đầu ra của bàn điều khiển sẽ làbefore immediate
after immediate
executing immediate: so immediate
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
7 trả về một đối tượng before immediate
after immediate
executing immediate: so immediate
4, có thể được sử dụng để hủy bỏ lịch trình ngay lập tức [xem before immediate
after immediate
executing immediate: so immediate
5 bên dưới]Đừng nhầm lẫn giữa
7 vớiconsole.log['before immediate']; setImmediate[[arg] => { console.log[`executing immediate: ${arg}`]; }, 'so immediate']; console.log['after immediate'];
7. Có một số cách chính họ khác nhau. Đầu tiên làbefore immediate after immediate executing immediate: so immediate
7 sẽ chạy trước bất kỳbefore immediate after immediate executing immediate: so immediate
4 nào được đặt cũng như trước bất kỳ I/O đã lên lịch nào. Thứ hai làbefore immediate after immediate executing immediate: so immediate
7 không thể xóa được, nghĩa là một khi mã đã được lên lịch để thực thi vớibefore immediate after immediate executing immediate: so immediate
7, quá trình thực thi sẽ không thể bị dừng lại, giống như với một chức năng bình thường. Tham khảo để hiểu rõ hơn về hoạt động củabefore immediate after immediate executing immediate: so immediate
7before immediate after immediate executing immediate: so immediate
Thực thi "Vòng lặp vô tận" ~ function intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
3
function intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
Nếu có một khối mã cần thực thi nhiều lần, thì có thể sử dụng
function intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
3 để thực thi mã đó. function intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
3 lấy một đối số hàm sẽ chạy vô số lần với độ trễ mili giây nhất định làm đối số thứ hai. Cũng giống như setTimeout[]
, các đối số bổ sung có thể được thêm sau độ trễ và chúng sẽ được chuyển đến lệnh gọi hàm. Cũng giống như setTimeout[]
, độ trễ không thể được đảm bảo do các hoạt động có thể tồn tại trong vòng lặp sự kiện và do đó nên được coi là độ trễ gần đúng. Xem ví dụ bên dướifunction intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
Trong ví dụ trên,
function intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
8 sẽ thực thi sau mỗi 1500 mili giây hoặc 1. 5 giây, cho đến khi nó dừng lại [xem bên dưới]Giống như setTimeout[]
,
function intervalFunc[] {
console.log['Cant stop me now!'];
}
setInterval[intervalFunc, 1500];
3 cũng trả về một đối tượng console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 có thể được sử dụng để tham chiếu và sửa đổi khoảng thời gian đã đặtXóa tương lai
Có thể làm gì nếu một đối tượng
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 hoặc before immediate
after immediate
executing immediate: so immediate
4 cần được hủy bỏ? . Bằng cách chuyển đối tượng đã nói vào hàm const timeoutObj = setTimeout[[] => {
console.log['timeout beyond time'];
}, 1500];
const immediateObj = setImmediate[[] => {
console.log['immediately executing immediate'];
}];
const intervalObj = setInterval[[] => {
console.log['interviewing the interval'];
}, 500];
clearTimeout[timeoutObj];
clearImmediate[immediateObj];
clearInterval[intervalObj];
9 tương ứng, việc thực thi đối tượng đó sẽ bị tạm dừng hoàn toàn. Các chức năng tương ứng là console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
5, before immediate
after immediate
executing immediate: so immediate
5 và const timerObj = setTimeout[[] => {
console.log['will i run?'];
}];
// if left alone, this statement will keep the above
// timeout from running, since the timeout will be the only
// thing keeping the program from exiting
timerObj.unref[];
// we can bring it back to life by calling ref[] inside
// an immediate
setImmediate[[] => {
timerObj.ref[];
}];
2. Xem ví dụ dưới đây để biết ví dụ về từngconst timeoutObj = setTimeout[[] => {
console.log['timeout beyond time'];
}, 1500];
const immediateObj = setImmediate[[] => {
console.log['immediately executing immediate'];
}];
const intervalObj = setInterval[[] => {
console.log['interviewing the interval'];
}, 500];
clearTimeout[timeoutObj];
clearImmediate[immediateObj];
clearInterval[intervalObj];
Bỏ lại thời gian chờ phía sau
Hãy nhớ rằng các đối tượng
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 được trả về bởi const timerObj = setTimeout[[] => {
console.log['will i run?'];
}];
// if left alone, this statement will keep the above
// timeout from running, since the timeout will be the only
// thing keeping the program from exiting
timerObj.unref[];
// we can bring it back to life by calling ref[] inside
// an immediate
setImmediate[[] => {
timerObj.ref[];
}];
4 và const timerObj = setTimeout[[] => {
console.log['will i run?'];
}];
// if left alone, this statement will keep the above
// timeout from running, since the timeout will be the only
// thing keeping the program from exiting
timerObj.unref[];
// we can bring it back to life by calling ref[] inside
// an immediate
setImmediate[[] => {
timerObj.ref[];
}];
5. Đối tượng console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 cung cấp hai chức năng nhằm tăng cường hành vi của console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 với console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
6 và const timerObj = setTimeout[[] => {
console.log['will i run?'];
}];
// if left alone, this statement will keep the above
// timeout from running, since the timeout will be the only
// thing keeping the program from exiting
timerObj.unref[];
// we can bring it back to life by calling ref[] inside
// an immediate
setImmediate[[] => {
timerObj.ref[];
}];
9. Nếu có một đối tượng console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 được lên lịch sử dụng hàm require[]
1, thì có thể gọi console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
6 trên đối tượng đó. Điều này sẽ thay đổi hành vi một chút và không gọi đối tượng console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 nếu đó là mã cuối cùng để thực thi. Đối tượng console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 sẽ không giữ tiến trình tồn tại, chờ thực thiTheo cách tương tự, một đối tượng
console.log['before immediate'];
setImmediate[[arg] => {
console.log[`executing immediate: ${arg}`];
}, 'so immediate'];
console.log['after immediate'];
4 đã có _____1_______6 được gọi trên nó có thể loại bỏ hành vi đó bằng cách gọi ___39_______9 trên cùng một đối tượng ____1_______4 đó, điều này sau đó sẽ đảm bảo việc thực thi của nó. Tuy nhiên, hãy lưu ý rằng điều này không khôi phục chính xác hành vi ban đầu vì lý do hiệu suất. Xem bên dưới để biết ví dụ về cả haiconst timerObj = setTimeout[[] => {
console.log['will i run?'];
}];
// if left alone, this statement will keep the above
// timeout from running, since the timeout will be the only
// thing keeping the program from exiting
timerObj.unref[];
// we can bring it back to life by calling ref[] inside
// an immediate
setImmediate[[] => {
timerObj.ref[];
}];
Xa hơn nữa trong Vòng lặp sự kiện
Còn nhiều điều nữa về Vòng lặp sự kiện và Bộ hẹn giờ so với hướng dẫn này đã trình bày. Để tìm hiểu thêm về nội bộ của Node. js Event Loop và cách Bộ hẹn giờ hoạt động trong quá trình thực thi, hãy xem Nút này. hướng dẫn js. nút. js Vòng lặp sự kiện, Bộ hẹn giờ và quy trình. nextTick[]