|
1 | | -### `setTimeout` and `setInterval` |
| 1 | +### `setTimeout`と`setInterval` |
2 | 2 |
|
3 | | -Since JavaScript is asynchronous, it is possible to schedule the execution of a |
4 | | -function by using the `setTimeout` and `setInterval` functions. |
| 3 | +JavaScriptは非同期なので、`setTimeout`と`setInterval`関数を使ってある関数の実行のスケジュールを決める事が可能です。 |
5 | 4 |
|
6 | | -> **Note:** Timeouts are **not** part of the ECMAScript Standard. They are |
7 | | -> implemented as part of the [DOM][1]. |
| 5 | +> **注意点:** タイムアウトはECMAScript標準の一部では**ありません**。 |
| 6 | +> これらは[DOM][1]の一部として実装されています。 |
8 | 7 |
|
9 | 8 | function foo() {} |
10 | | - var id = setTimeout(foo, 1000); // returns a Number > 0 |
| 9 | + var id = setTimeout(foo, 1000); // Number > 0を返す |
11 | 10 |
|
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**. |
| 11 | +`setTimeout`が呼ばれた時に、タイムアウトのIDと`foo`この先の**おおよそ**1000msに実行するスケジュールを返します。`foo`は正確に**1度**だけ実行されます。 |
15 | 12 |
|
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. |
| 13 | +コードが実行されているJavaScriptエンジンのタイマー分解能によって決まります。この事実はJavaScriptがシングルスレッドのなので、他のスレッドでの実行を妨害してしまう事があるかもしれません。これは、`setTimeout`の呼び出しにより指定された正確なディレイで実行するという確実な賭けという**意味ではありません**。 |
20 | 14 |
|
21 | | -The function that was passed as the first parameter will get called by the |
22 | | -*global object*, that means, that [`this`](#function.this) inside the called function |
23 | | -refers to that very object. |
| 15 | +第一パラメーターを渡された関数は*グローバルオブジェクト*によって呼び出されます。これは呼び出された関数の内部で[`this`](#functionis)がまさにこのオブジェクトを参照しているという事になります。 |
24 | 16 |
|
25 | 17 | function Foo() { |
26 | 18 | this.value = 42; |
27 | 19 | this.method = function() { |
28 | | - // this refers to the global object |
29 | | - console.log(this.value); // will log undefined |
| 20 | + // これはグローバルオブジェクトを参照しています |
| 21 | + console.log(this.value); // undefinedを記録するはずです |
30 | 22 | }; |
31 | 23 | setTimeout(this.method, 500); |
32 | 24 | } |
33 | 25 | new Foo(); |
34 | 26 |
|
35 | 27 |
|
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 |
38 | | -> **return value** of the call `foo` and **not** `foo`. This is, most of the time, |
39 | | -> a silent error, since when the function returns `undefined` `setTimeout` will |
40 | | -> **not** raise any error. |
| 28 | +> **注意点:** `setTimeout`は**関数オブジェクト**を第一引数に取ります。 |
| 29 | +> 良く間違えてしまう使い方として`setTimeout(foo(), 1000)`というものがあります。 |
| 30 | +> `foo`と`foo`**以外**の呼び出しに対する**戻り値**としてしまいます。これは、大体において、 |
| 31 | +> 関数が`undefined`になる為に表に出ないエラーになるでしょう。`setTimeout`はどんな |
| 32 | +> エラーも発生`させません`。 |
41 | 33 |
|
42 | 34 | ### Stacking Calls with `setInterval` |
43 | 35 |
|
|
0 commit comments