Skip to content

Commit 5f55365

Browse files
Clean up language in the other section.
1 parent d9aeb54 commit 5f55365

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

doc/en/other/timeouts.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
### `setTimeout` and `setInterval`
22

33
Since JavaScript is asynchronous, it is possible to schedule the execution of a
4-
function by using the `setTimeout` and `setInterval` functions.
4+
function using the `setTimeout` and `setInterval` functions.
55

66
> **Note:** Timeouts are **not** part of the ECMAScript Standard. They are
77
> implemented as part of the [DOM][1].
88
99
function foo() {}
1010
var id = setTimeout(foo, 1000); // returns a Number > 0
1111

12-
When `setTimeout` gets called, it will return the ID of the timeout and schedule
13-
`foo` to run in **approximately** one thousand milliseconds in the future.
14-
`foo` will then get executed exactly **once**.
12+
When `setTimeout` is called, it returns the ID of the timeout and schedule
13+
`foo` to run **approximately** one thousand milliseconds in the future.
14+
`foo` will then be executed **once**.
1515

16-
Depending on the timer resolution of the JavaScript engine that is running the
17-
code, as well as the fact that JavaScript is single threaded and other code that
18-
gets executed might block the thread, it is by **no means** a safe bet that one
19-
will get the exact delay that was specified in the `setTimeout` call.
16+
Depending on the timer resolution of the JavaScript engine running the code, as
17+
well as the fact that JavaScript is single threaded and other code that gets
18+
executed might block the thread, it is by **no means** a safe bet that one will
19+
get the exact delay specified in the `setTimeout` call.
2020

2121
The function that was passed as the first parameter will get called by the
2222
*global object*, which means that [`this`](#function.this) inside the called function
23-
refers to that very object.
23+
refers to the global object.
2424

2525
function Foo() {
2626
this.value = 42;
@@ -34,7 +34,7 @@ refers to that very object.
3434

3535

3636
> **Note:** As `setTimeout` takes a **function object** as its first parameter, an
37-
> often made mistake is to use `setTimeout(foo(), 1000)`, which will use the
37+
> common mistake is to use `setTimeout(foo(), 1000)`, which will use the
3838
> **return value** of the call `foo` and **not** `foo`. This is, most of the time,
3939
> a silent error, since when the function returns `undefined` `setTimeout` will
4040
> **not** raise any error.
@@ -72,29 +72,31 @@ the function itself.
7272
foo();
7373

7474
Not only does this encapsulate the `setTimeout` call, but it also prevents the
75-
stacking of calls and it gives additional control. `foo` itself can now decide
75+
stacking of calls and gives additional control. `foo` itself can now decide
7676
whether it wants to run again or not.
7777

7878
### Manually Clearing Timeouts
7979

8080
Clearing timeouts and intervals works by passing the respective ID to
81-
`clearTimeout` or `clearInterval`, depending which `set` function was used in
82-
the first place.
81+
`clearTimeout` or `clearInterval`, depending on which `set` function was used
82+
in the first place.
8383

8484
var id = setTimeout(foo, 1000);
8585
clearTimeout(id);
8686

87-
### Clearing all timeouts
87+
### Clearing All Timeouts
8888

89-
Because there is no built-in method for clearing all timeouts and/or intervals,
89+
As there is no built-in method for clearing all timeouts and/or intervals,
9090
it is necessary to use brute force in order to achieve this functionality.
9191

9292
// clear "all" timeouts
9393
for(var i = 1; i < 1000; i++) {
9494
clearTimeout(i);
9595
}
9696

97-
But there might still be timeouts that are unaffected by this arbitrary number. Another way of doing this is to consider that the ID given to a timeout is incremented by one everytime you call `setTimeout`.
97+
But there might still be timeouts that are unaffected by this arbitrary number.
98+
Another way of doing this is to consider that the ID given to a timeout is
99+
incremented by one every time you call `setTimeout`.
98100

99101
// clear "all" timeouts
100102
var biggestTimeoutId = window.setTimeout(function(){}, 1),
@@ -103,17 +105,19 @@ But there might still be timeouts that are unaffected by this arbitrary number.
103105
clearTimeout(i);
104106
}
105107

106-
But even though this works on all main browsers nowadays, it isn't specified that the IDs should be ordered that way and it may change. Therefore, it is instead recommended to keep track of all the timeout IDs, so
107-
they can be cleared specifically.
108+
Even though this works on all major browsers today, it isn't specified that
109+
the IDs should be ordered that way and it may change. Therefore, it is instead
110+
recommended to keep track of all the timeout IDs, so they can be cleared
111+
specifically.
108112

109-
### Hidden use of `eval`
113+
### Hidden Use of `eval`
110114

111115
`setTimeout` and `setInterval` can also take a string as their first parameter.
112116
This feature should **never** be used because it internally makes use of `eval`.
113117

114118
> **Note:** Since the timeout functions are **not** specified by the ECMAScript
115119
> standard, the exact workings when a string is passed to them might differ in
116-
> various JavaScript implementations. For example, Microsoft's JScript makes use of
120+
> various JavaScript implementations. For example, Microsoft's JScript uses
117121
> the `Function` constructor in place of `eval`.
118122
119123
function foo() {
@@ -129,10 +133,10 @@ This feature should **never** be used because it internally makes use of `eval`.
129133
bar();
130134

131135
Since `eval` is not getting called [directly](#core.eval) in this case, the string
132-
passed to `setTimeout` will get executed in the *global scope*; thus, it will
136+
passed to `setTimeout` will be executed in the *global scope*; thus, it will
133137
not use the local variable `foo` from the scope of `bar`.
134138

135-
It is further recommended to **not** use a string for passing arguments to the
139+
It is further recommended to **not** use a string to pass arguments to the
136140
function that will get called by either of the timeout functions.
137141

138142
function foo(a, b, c) {}
@@ -151,7 +155,7 @@ function that will get called by either of the timeout functions.
151155
152156
### In Conclusion
153157

154-
**Never** should a string be used as the parameter of `setTimeout` or
158+
A string should **never** be used as the parameter of `setTimeout` or
155159
`setInterval`. It is a clear sign of **really** bad code, when arguments need
156160
to be supplied to the function that gets called. An *anonymous function* should
157161
be passed that then takes care of the actual call.

0 commit comments

Comments
 (0)