You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/en/other/timeouts.md
+27-23Lines changed: 27 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
1
### `setTimeout` and `setInterval`
2
2
3
3
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.
5
5
6
6
> **Note:** Timeouts are **not** part of the ECMAScript Standard. They are
7
7
> implemented as part of the [DOM][1].
8
8
9
9
function foo() {}
10
10
var id = setTimeout(foo, 1000); // returns a Number > 0
11
11
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**.
15
15
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.
20
20
21
21
The function that was passed as the first parameter will get called by the
22
22
*global object*, which means that [`this`](#function.this) inside the called function
23
-
refers to that very object.
23
+
refers to the global object.
24
24
25
25
function Foo() {
26
26
this.value = 42;
@@ -34,7 +34,7 @@ refers to that very object.
34
34
35
35
36
36
> **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
38
38
> **return value** of the call `foo` and **not**`foo`. This is, most of the time,
39
39
> a silent error, since when the function returns `undefined``setTimeout` will
40
40
> **not** raise any error.
@@ -72,29 +72,31 @@ the function itself.
72
72
foo();
73
73
74
74
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
76
76
whether it wants to run again or not.
77
77
78
78
### Manually Clearing Timeouts
79
79
80
80
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.
83
83
84
84
var id = setTimeout(foo, 1000);
85
85
clearTimeout(id);
86
86
87
-
### Clearing all timeouts
87
+
### Clearing All Timeouts
88
88
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,
90
90
it is necessary to use brute force in order to achieve this functionality.
91
91
92
92
// clear "all" timeouts
93
93
for(var i = 1; i < 1000; i++) {
94
94
clearTimeout(i);
95
95
}
96
96
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`.
98
100
99
101
// clear "all" timeouts
100
102
var biggestTimeoutId = window.setTimeout(function(){}, 1),
@@ -103,17 +105,19 @@ But there might still be timeouts that are unaffected by this arbitrary number.
103
105
clearTimeout(i);
104
106
}
105
107
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.
108
112
109
-
### Hidden use of `eval`
113
+
### Hidden Use of `eval`
110
114
111
115
`setTimeout` and `setInterval` can also take a string as their first parameter.
112
116
This feature should **never** be used because it internally makes use of `eval`.
113
117
114
118
> **Note:** Since the timeout functions are **not** specified by the ECMAScript
115
119
> 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
117
121
> the `Function` constructor in place of `eval`.
118
122
119
123
function foo() {
@@ -129,10 +133,10 @@ This feature should **never** be used because it internally makes use of `eval`.
129
133
bar();
130
134
131
135
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
133
137
not use the local variable `foo` from the scope of `bar`.
134
138
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
136
140
function that will get called by either of the timeout functions.
137
141
138
142
function foo(a, b, c) {}
@@ -151,7 +155,7 @@ function that will get called by either of the timeout functions.
151
155
152
156
### In Conclusion
153
157
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
155
159
`setInterval`. It is a clear sign of **really** bad code, when arguments need
156
160
to be supplied to the function that gets called. An *anonymous function* should
157
161
be passed that then takes care of the actual call.
0 commit comments