Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Vishal-K-988
Copy link
Contributor

@Vishal-K-988 Vishal-K-988 commented May 18, 2025

Description

This PR updates the JavaScript Timers documentation to accurately reflect the Node.js-specific behavior of setTimeout() and setInterval(). The changes include:

  • Clarified that these functions return a Timeout object in Node.js (vs. numeric ID in browsers)
  • Added documentation for the Timeout object's additional methods:
    • ref(): Keeps the Node.js event loop active while the timer is running
    • unref(): Allows the Node.js event loop to exit even if the timer is still running
    • refresh(): Resets the timer's start time to the current time
  • Maintained consistent mentions of browser vs Node.js behavior throughout the document
  • Preserved existing examples and diagrams while enhancing explanations

These 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:

  1. Reviewing the updated content for accuracy and clarity
  2. Ensuring the documentation maintains its existing structure and readability
  3. Verifying that all code examples remain functional
  4. Confirming that the changes align with the Node.js Timers module documentation

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() and setInterval().
Issue: #7672

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run pnpm format to ensure the code follows the style guide.
  • I have run pnpm test to check if all tests are passing.
  • I have run pnpm build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

…object in Nodejs and ID when executed in Browser

Signed-off-by: vishal <[email protected]>
@Vishal-K-988 Vishal-K-988 requested a review from a team as a code owner May 18, 2025 18:39
Copy link

vercel bot commented May 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview May 18, 2025 6:40pm

@@ -130,6 +138,6 @@ to achieve this scenario:

![Recursive setTimeout](/static/images/learn/javascript-timers/recursive-settimeout.png)

`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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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.

Comment on lines +96 to +97
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()`).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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(() => {
Copy link
Member

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

Suggested change
const id = setTimeout(() => {
const timeout = setTimeout(() => {

Comment on lines +47 to +52
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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants