Skip to content

Commit eddf3a6

Browse files
committed
doc: clarify setTimeout() return type in node
1 parent a356572 commit eddf3a6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

apps/site/pages/en/learn/asynchronous-work/discover-javascript-timers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ const myFunction = (firstParam, secondParam) => {
3333
setTimeout(myFunction, 2000, firstParam, secondParam);
3434
```
3535

36-
`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:
36+
`setTimeout` returns a [`Timeout`](https://nodejs.org/api/timers.html#class-timeout) instance in Node.js, whereas in browsers it returns a numeric timer ID. This object or ID can be used to cancel the scheduled function execution:
3737

3838
```js
39-
const id = setTimeout(() => {
39+
const timeout = setTimeout(() => {
4040
// should run after 2 seconds
4141
}, 2000);
4242

4343
// I changed my mind
44-
clearTimeout(id);
44+
clearTimeout(timeout);
4545
```
4646

4747
### Zero delay
@@ -80,11 +80,11 @@ setInterval(() => {
8080
The function above runs every 2 seconds unless you tell it to stop, using `clearInterval`, passing it the interval id that `setInterval` returned:
8181

8282
```js
83-
const id = setInterval(() => {
83+
const timeout = setInterval(() => {
8484
// runs every 2 seconds
8585
}, 2000);
8686

87-
clearInterval(id);
87+
clearInterval(timeout);
8888
```
8989

9090
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`:

0 commit comments

Comments
 (0)