-
-
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?
Added details about setTimeout() and setInterval() returns a Timeout … #7759
Conversation
…object in Nodejs and ID when executed in Browser Signed-off-by: vishal <[email protected]>
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
`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. | |
`setTimeout` and `setInterval` are available in Node.js, through the [Timers module](https://nodejs.org/api/timers.html). |
We've already said this earlier in the document.
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()`). | ||
|
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.
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()`). |
Earlier, we say "setInterval
is a function similar to setTimeout
", which I think implies they have the same return.
@@ -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: |
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.
`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: | |
`setTimeout` returns a [`Timeout`](https://nodejs.org/api/timers.html#class-timeout) instance in Node.js, whereas in browsers it returns a numeric ID. This object or ID can be used to cancel the scheduled function execution: |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
These example should probably not use id
, they are meant for Node.js, so let's use a more specific variable name
const id = setTimeout(() => { | |
const timeout = setTimeout(() => { |
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 | ||
|
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.
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 |
See my other suggestion. We can link to the documentation.
Description
This PR updates the JavaScript Timers documentation to accurately reflect the Node.js-specific behavior of
setTimeout()
andsetInterval()
. The changes include:ref()
: Keeps the Node.js event loop active while the timer is runningunref()
: Allows the Node.js event loop to exit even if the timer is still runningrefresh()
: Resets the timer's start time to the current timeThese changes help prevent confusion for developers who might expect numeric IDs and make them aware of the additional functionality available in Node.js.
Validation
The changes have been validated by:
Related Issues
This PR addresses the documentation issue regarding the misleading description of timer return values in Node.js. It helps clarify the distinction between browser and Node.js behavior for
setTimeout()
andsetInterval()
.Issue: #7672
Check List
pnpm format
to ensure the code follows the style guide.pnpm test
to check if all tests are passing.pnpm build
to check if the website builds without errors.