73
73
clearTimeout(i);
74
74
}
75
75
76
- 可能還有一些定石器不會在上面的代碼中被清除 ,因此我們可以事先保存所有的定時器 ID,然後一把清除。
76
+ 可能還有一些定時器不會在上面的代碼中被清除 ,因此我們可以事先保存所有的定時器 ID,然後一把清除。
77
77
78
78
79
79
// clear "all" timeouts
@@ -105,15 +105,15 @@ This feature should **never** be used because it internally makes use of `eval`.
105
105
}
106
106
bar();
107
107
108
- Since ` eval ` is not getting called [ directly] ( #core.eval ) in this case, the string
109
- passed to ` setTimeout ` will be executed in the * global scope* ; thus, it will
108
+ Since ` eval ` is not getting called [ directly] ( #core.eval ) in this case, the string
109
+ passed to ` setTimeout ` will be executed in the * global scope* ; thus, it will
110
110
not use the local variable ` foo ` from the scope of ` bar ` .
111
111
112
112
It is further recommended to ** not** use a string to pass arguments to the
113
- function that will get called by either of the timeout functions.
113
+ function that will get called by either of the timeout functions.
114
114
115
115
function foo(a, b, c) {}
116
-
116
+
117
117
// NEVER use this
118
118
setTimeout('foo(1, 2, 3)', 1000)
119
119
@@ -122,19 +122,18 @@ function that will get called by either of the timeout functions.
122
122
foo(a, b, c);
123
123
}, 1000)
124
124
125
- > ** Note:** While it is also possible to use the syntax
125
+ > ** Note:** While it is also possible to use the syntax
126
126
> ` setTimeout(foo, 1000, a, b, c) ` , it is not recommended, as its use may lead
127
- > to subtle errors when used with [ methods] ( #function.this ) .
127
+ > to subtle errors when used with [ methods] ( #function.this ) .
128
128
129
129
### In Conclusion
130
130
131
- A string should ** never** be used as the parameter of ` setTimeout ` or
132
- ` setInterval ` . It is a clear sign of ** really** bad code, when arguments need
131
+ A string should ** never** be used as the parameter of ` setTimeout ` or
132
+ ` setInterval ` . It is a clear sign of ** really** bad code, when arguments need
133
133
to be supplied to the function that gets called. An * anonymous function* should
134
134
be passed that then takes care of the actual call.
135
135
136
136
Furthermore, the use of ` setInterval ` should be avoided because its scheduler is not
137
137
blocked by executing JavaScript.
138
138
139
139
[ 1 ] : http://en.wikipedia.org/wiki/Document_Object_Model " Document Object Model "
140
-
0 commit comments