1
1
### ` setTimeout ` 和 ` setInterval `
2
2
3
- 由於 Javascript 是一個非同步傳輸的系統,因此可以執行一個函式用 ` setTimeout ` 和 ` setInterval ` 。
3
+ 由於 Javascript 具有非同步的特性,因此可以用 ` setTimeout ` 和 ` setInterval ` 來執行一個函式 。
4
4
5
5
> ** 注意:** Timeouts 不在 ECMAScript 的標準中。它們是 [ DOM] [ 1 ] 其中的一部分
6
6
7
7
function foo() {}
8
8
var id = setTimeout(foo, 1000); // returns a Number > 0
9
9
10
- 當 ` setTimeout ` 被呼叫,它會回傳一個 ID 標準並是計畫在將來 ** 大約** 1000 毫秒後在在去呼叫 ` foo ` 函式。
10
+ 當 ` setTimeout ` 被呼叫,它會回傳一個 ID 標準並且 ** 大約** 1000 毫秒後在在去呼叫 ` foo ` 函式。
11
11
` foo ` 函式只會被執行 ** 一次** 。
12
12
13
13
基於 JavaScript 引擎的計時策略,以及基本的單線程運行的方式,所以其他的程式碼可以被阻塞。
85
85
86
86
### 隱藏使用 ` eval `
87
87
88
- ` setTimeout ` and ` setInterval ` can also take a string as their first parameter.
89
- This feature should ** never** be used because it internally makes use of ` eval ` .
88
+ ` setTimeout ` and ` setInterval ` 也可以使用字串當作他們的第一個參數.
89
+ 不過這個特性 ** 絕對** 不要使用, 因為在內部他將利用 ` eval ` 來實作。
90
+
91
+ > ** 注意:** 由於 timeout 函式 ** 並未** 被列入 ECMAScript
92
+ > 標準中,當你將字串當成參數傳入時,在不同的 Javscript
93
+ > 實作中很有可能會造成不一樣的行為。比如說:在 Microsoft 的 JScript 中,就使用 ` Function `
94
+ > 建構子來取代 ` eval ` 。
95
+
90
96
91
- > ** Note:** Since the timeout functions are ** not** specified by the ECMAScript
92
- > standard, the exact workings when a string is passed to them might differ in
93
- > various JavaScript implementations. For example, Microsoft's JScript uses
94
- > the ` Function ` constructor in place of ` eval ` .
95
97
96
98
function foo() {
97
99
// will get called
@@ -105,12 +107,10 @@ This feature should **never** be used because it internally makes use of `eval`.
105
107
}
106
108
bar();
107
109
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
- not use the local variable ` foo ` from the scope of ` bar ` .
110
+ 在這個範例中,由於 ` eval ` 沒有被 [ 直接 ] ( #core.eval ) 呼叫,在 ` setTimeout ` 中被傳入的字串將會在 * 全域 * 範圍中被執行,因此,他將不會使用在 ` bar ` 區域的 ` foo ` 。
111
+
112
+ 我們進一步建議 ** 不要 ** 用字串當作參數傳到會被 timeout 呼叫的函式中。
111
113
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.
114
114
115
115
function foo(a, b, c) {}
116
116
@@ -122,18 +122,14 @@ function that will get called by either of the timeout functions.
122
122
foo(1, 2, 3);
123
123
}, 1000)
124
124
125
- > ** Note: ** While it is also possible to use the syntax
126
- > ` setTimeout(foo, 1000, 1, 2, 3) ` , it is not recommended, as its use may lead
127
- > to subtle errors when used with [ methods ] ( #function.this ) .
125
+ > ** 注意 ** 儘管使用 ` setTimeout(foo, 1000, 1, 2, 3) `
126
+ > 這樣的文法是可能的,但我們卻不建議這樣做,因為這和 [ 方法 ] ( #function.this )
127
+ > 一起使用時可能會導致微妙的錯誤。
128
128
129
- ### In Conclusion
129
+ ### 結論
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
133
- to be supplied to the function that gets called. An * anonymous function* should
134
- be passed that then takes care of the actual call.
131
+ ** 絕對** 不要使用字串當作 ` setTimeout ` 或 ` setInterval ` 參數。當參數要被當成呼叫的函式時,這絕對是 ** 不好** 的程式碼,相反的,利用 * 匿名函式* 來完成這樣的行為。
135
132
136
- Furthermore, the use of ` setInterval ` should be avoided because its scheduler is not
137
- blocked by executing JavaScript.
133
+ 此外,應該避免使用 ` setInterval ` ,因為他將不會被 Javascript 給中斷。
138
134
139
135
[ 1 ] : http://en.wikipedia.org/wiki/Document_Object_Model " Document Object Model "
0 commit comments