-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Added details about setTimeout() and setInterval() returns a Timeout … #7759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,7 +33,7 @@ const myFunction = (firstParam, secondParam) => { | |||||||||||
setTimeout(myFunction, 2000, firstParam, secondParam); | ||||||||||||
``` | ||||||||||||
|
||||||||||||
`setTimeout` returns the timer id. This is generally not used, but you can store this id, and clear it if you want to delete this scheduled function execution: | ||||||||||||
`setTimeout` returns a timer identifier. In Node.js, this is a Timeout object that provides additional functionality, while in browsers it returns a numeric ID. You can store this identifier and use it to cancel the scheduled function execution: | ||||||||||||
|
||||||||||||
```js | ||||||||||||
const id = setTimeout(() => { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These example should probably not use
Suggested change
|
||||||||||||
|
@@ -44,6 +44,12 @@ const id = setTimeout(() => { | |||||||||||
clearTimeout(id); | ||||||||||||
``` | ||||||||||||
|
||||||||||||
In Node.js, the Timeout object provides additional methods: | ||||||||||||
|
||||||||||||
- `timeout.ref()`: Keeps the Node.js event loop active while the timer is running | ||||||||||||
- `timeout.unref()`: Allows the Node.js event loop to exit even if the timer is still running | ||||||||||||
- `timeout.refresh()`: Resets the timer's start time to the current time | ||||||||||||
|
||||||||||||
Comment on lines
+47
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
See my other suggestion. We can link to the documentation. |
||||||||||||
### Zero delay | ||||||||||||
|
||||||||||||
If you specify the timeout delay to `0`, the callback function will be executed as soon as possible, but after the current function execution: | ||||||||||||
|
@@ -87,6 +93,8 @@ const id = setInterval(() => { | |||||||||||
clearInterval(id); | ||||||||||||
``` | ||||||||||||
|
||||||||||||
Like `setTimeout`, `setInterval` returns a Timeout object in Node.js (and a numeric ID in browsers). This object provides the same additional methods (`ref()`, `unref()`, and `refresh()`). | ||||||||||||
|
||||||||||||
Comment on lines
+96
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Earlier, we say " |
||||||||||||
It's common to call `clearInterval` inside the setInterval callback function, to let it auto-determine if it should run again or stop. For example this code runs something unless App.somethingIWait has the value `arrived`: | ||||||||||||
|
||||||||||||
```js | ||||||||||||
|
@@ -130,6 +138,6 @@ to achieve this scenario: | |||||||||||
|
||||||||||||
 | ||||||||||||
|
||||||||||||
`setTimeout` and `setInterval` are available in Node.js, through the [Timers module](https://nodejs.org/api/timers.html). | ||||||||||||
`setTimeout` and `setInterval` are available in Node.js, through the [Timers module](https://nodejs.org/api/timers.html). In Node.js, these functions return a Timeout object that provides additional functionality for timer management. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We've already said this earlier in the document. |
||||||||||||
|
||||||||||||
Node.js also provides `setImmediate()`, which is equivalent to using `setTimeout(() => {}, 0)`, mostly used to work with the Node.js Event Loop. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.