setTimeout()

Window オブジェクト setTimeout、setInterval メソッド (DOM)

[45] setTimeout()setInterval() は、一定時間後、あるいは一定時間ごとに指定した処理を実行するものです。

仕様書

定義

[46] WindowOrWorkerGlobalScope インターフェイスは、メソッド setTimeout, setInterval, clearTimeout, clearInterval を持ちます >>33WindowOrWorkerGlobalScopeWindow >>33WorkerGlobalScope >>47実装しています。

タイマー設定メソッド

[48] setTimeoutsetInterval は、 いずれもタイマー初期化手順を実行するものです >>33

[49] 第1引数
一定時間後に実行されるべきコードを指定します。データ型TimerHandler、すなわち Function または DOMString です >>33。 この引数は必要です >>33
[50] 第2引数
コードが実行されるまでの時間です。データ型long で、省略した場合の既定値0 です >>33
[51] 第3引数以降
任意の値を指定でき >>33、コードへの引数として使われます。

[52] メソッドの返り値は、 long です >>33

待ち時間

[87] 与えられたコードは、 >>66>>72>>32 を待った後、実行されます。

[66] 基本となる待ち時間は、第2引数で指定された値です。ただしその値が4より小さく、 タイマー入れ子水準 (なければ0) が5より大きいなら、4とします。 >>33

[75] タイマー入れ子水準 (timer nesting level) >>33 は、 入れ子回数の管理のための値です。タイマー初期化手順により追加されたタスクに存在します。 追加されたタスクタイマー入れ子水準は、元のタスクタイマー入れ子水準よりも1大きな値です >>33

[31] 元々は最小でも 10ms とされていましたが、後に実装によっては最小 4ms となり、 >>27 で仕様もそれに揃いました。更に入れ子回数 (タイマーによる処理によって更にタイマー処理を予定した回数) が4回までは 4ms の制限すらも撤廃されるようになり、仕様も >>24 で追随しています。

[70] Window では、 関連付けられている文書完全に活性な状態のミリ秒数として解釈されます >>33

[71] WorkerGlobalScope では、ワーカーsuspend されていない状態のミリ秒数として解釈されます >>33

[86] いずれも、連続的な秒数ではなく、合計の秒数です。

[72] 同じ文脈オブジェクトについてタイマー初期化手順が何度も呼ばれている場合、 呼び出しのタイミングがより早く、待ち時間 (>>66) が等しいか小さいものが実行完了 (タスクが追加完了) されるまで、待つことになっています >>33

[32] 利用者エージェントは更に加えて任意の時間待っても構いません >>33電力等利用できる資源の制約や性能上の限界その他の要因により処理を遅らせることができます。

[130] Internet Explorer のタイマーの動作の設定が Windows コントロールパネルの省電力設定内にあります、 バッテリー駆動中か電源接続中かでも動作を変更できます。

活性タイマーのリスト

[55] WindowOrWorkerGlobalScope オブジェクト (WindowWorkerGlobalScope) は活性タイマー群のリスト (list of active timers) を持ちます >>33活性タイマーのリストには、 setTimeoutsetInterval で指定されたタイマーが含まれます。

[60] 活性タイマーのリスト内の各項目は、非負整数により識別されます >>33。 この非負整数ハンドルと呼ばれています。

[88] ハンドルは、 WindowOrWorkerGlobalScope オブジェクトの生存期間内において固有でなければなりません >>33

[56] ということは long で表せるの回数以上はタイマーを使えないことになります。

[89] ハンドルタイマー初期化手順の実行時 (setInterval の場合は、初回のみ) に利用者エージェント定義の方法で決定されます >>33タイマー初期化手順はこのハンドルを使って活性タイマーのリストに新しい項目を追加し、 そのハンドルを返します >>33

[4] このリストは、unloading document cleanup steps によって空にされることがあります。

[123] 活性タイマーのリスト内のタイマーが次にいつ実行されるかは、 アイドル期間の締切の決定に影響する可能性があります。

タイマーの実行

[74] WindowOrWorkerGlobalScope インターフェイスsetTimeout メソッドsetInterval メソッドは、次のようにしなければなりません >>33

  1. [76] 取り扱い器を、第1引数を必須の TimerHandler として解釈した結果に設定します。
  2. [79] タイムアウトを、第2引数を long として解釈した結果に設定します。 既定値は 0 とします。
  3. [81] 引数群を、第3引数以後すべての引数に設定します。
  4. [95] 繰り返しを、setInterval メソッドならsetTimeout メソッドならに設定します。
  5. [94] 文脈オブジェクト取り扱い器タイムアウト引数群繰り返しについて、 タイマー初期化手順を実行した結果を返します。 以前のハンドルは、null とします。

[59] タイマー初期化手順 (timer initialization steps) は、 文脈取り扱い器タイムアウト引数群繰り返し以前のハンドルについて、次のようにします >>33

  1. [73] 文脈Window なら、
    1. [98] 文脈を、文脈WindowProxy に設定します。
  2. [69] 以前のハンドルnull なら、
    1. [82] ハンドルを、文脈活性タイマーのリストのどの項目のハンドルとも異なる利用者エージェント定義の正整数に設定します。
    2. [90] 文脈活性タイマーのリストに、ハンドルハンドルの項目を追加します。
  3. [83] それ以外なら、
    1. [84] ハンドルを、以前のハンドルに設定します。
  4. [96] 呼び出し元realmを、現在Realm記録に設定します。
  5. [97] 呼び出し先realmを、文脈Realmに設定します。
  6. [134] 元スクリプトを、活性スクリプトに設定します。
  7. [128] タイムアウトの場合、
    1. [129] タイムアウトを、0 に設定します。
  8. [104] 現在走っているタスクがタイマータスクなら、
    1. [105] 入れ子水準を、現在走っているタスクタイマー入れ子水準に設定します。
  9. [106] それ以外なら、
    1. [107] 入れ子水準を、 0 に設定します。
  10. [108] 入れ子水準5 より大きく、タイムアウト4 より小さいなら、
    1. [109] タイムアウトを、 4 に設定します。
  11. [110] 入れ子水準を、入れ子水準 + 1 に設定します。
  12. [99] タスクを、ハンドル文脈取り扱い器タイムアウト引数群繰り返しフラグ、呼び出し元realm呼び出し先realm入れ子水準に関するタイマータスク (>>77) に設定します。
  13. [112] 文脈においてタイムアウト後のタスクの実行を予約します (>>113)。
  14. [111] ハンドルを返します。

[77] ハンドル文脈取り扱い器引数群繰り返しフラグ、 呼び出し元realm呼び出し先realm入れ子水準に関するタイマータスクは、次のようなものです >>33

処理
  1. [63] 文脈活性タイマーのリストの、 ハンドルハンドルの項目の消去済みフラグが設定されていれば、
    1. [100] ここで停止します。
  2. [62] 取り扱い器により、
    [78] Function
    コールバック関数の呼び出しを実行します。
    コールバック関数
    取り扱い器
    コールバックthis
    文脈
    [80] DOMString
    1. [101] HostEnsureCanCompileStrings(呼び出し元realm呼び出し先realm) を実行します。例外投げられたら、例外を報告し、 ここで停止します。
    2. [135] 基底URLを、 元スクリプト基底URLに設定します。
    3. [136] オプション群を、 新しいスクリプトfetchオプション群に設定します。
      [137] スクリプトfetchオプション群
      暗号学的nonce
      元スクリプトfetchオプション群暗号学的nonce
      一貫性メタデータ
      空文字列
      構文解析器メタデータ
      not-parser-inserted
      credentialsモード
      元スクリプトfetchオプション群credentialsモード
      参照元ポリシー
      元スクリプトfetchオプション群参照元ポリシー
    4. [102] スクリプトを、古典スクリプトの作成を実行した結果に設定します。
      ソーステキスト
      取り扱い器
      設定群オブジェクト
      文脈環境設定群オブジェクト
      基底URL
      文脈環境設定群オブジェクトAPI基底URL
      オプション群
      オプション群
    5. [103] スクリプトについて古典スクリプトの実行をします。
  3. [65] 繰り返しフラグがなら、 タイマー初期化手順を実行します。 文脈取り扱い器タイムアウト引数群を引き渡します。 繰り返しフラグは以前のハンドルハンドルとします。
タスク源
タイマータスク源 (timer task source)
タイマー入れ子水準
入れ子水準

[85] setInterval文字列取り扱い器として指定した場合、 毎回スクリプトのコンパイルから行われることになります。

[113] 文脈においてタイムアウト後にタスクを実行するとは、 文脈実行待ちタイマー群の末尾に新しい組 (タスク, タイムアウト) を追加して、起点を現在時刻として、 並列に、次のすべての条件が満たされるまで待ちます >>33

[120] 条件が満たされたら、組 (タスク, タイムアウト) を文脈実行待ちタイマー群から削除し、 タスクタスクキューに追加します >>33

タイマー消去メソッド

[53] clearTimeoutclearInterval は、いずれも、活性タイマーのリストから引数で指定されたにより識別される項目があれば、 これを消去します。該当するものがなければ、何もしません。 >>33

[57] 第1引数
long引数を1つ指定できます >>33。省略した場合の既定値0 です >>33

[54] メソッドの返り値はありません >>33

[91] WindowOrWorkerGlobal インターフェイスclearTimeout メソッドclearInterval メソッドは、次のようにしなければりません >>33

  1. [92] ハンドルを、第1引数を long として解釈した結果に設定します。 既定値は 0 とします。
  2. [93] 文脈オブジェクト活性タイマーのリストに、 ハンドルハンドルの項目があれば、 その消去済みフラグを設定します。

[58] どちらのメソッドも、同じように動作します。 repeat フラグは無視されます。

歴史

[2] IRC logs: freenode / #whatwg / 20070409 (2007-04-12 00:04:42 +09:00 版) http://krijnhoetmer.nl/irc-logs/whatwg/20070409#l-233 (名無しさん 2007-04-11 15:37:28 +00:00)

[6] Re: Proposal: High resolution (and otherwise improved) timer API (Maciej Stachowiak 著, 2008-10-04 06:21:35 +09:00 版) http://lists.w3.org/Archives/Public/public-webapps/2008OctDec/0031.html

I cannot state with certainty that nothing lower than 10ms is safe. Chrome shipped with a 1ms delay and that was found to create problems on a number of sites, including nytimes. They are planning to try 4ms next. We would consider using a lower limit in the official webkit.org version of WebKit, not not as low as 1ms.

[9] IRC logs: freenode / #whatwg / 20090504 ( 版) http://krijnhoetmer.nl/irc-logs/whatwg/20090504#l-24

[10] [webkit-dev] setTimeout as browser speed throttle ( 版) http://lists.macosforge.org/pipermail/webkit-dev/2008-September/005093.html

[12] Web Applications 1.0 r5535 Allow timers to be delayed when power usage is an issue.Fixing http://www.w3.org/Bugs/Public/show_bug.cgi?id=10633 ( ( 版)) http://html5.org/tools/web-apps-tracker?from=5534&to=5535

[13] Bug 10633 – Minimums for timer granularity should be left to the user agent - different form factors have different power requirements ( ( 版)) http://www.w3.org/Bugs/Public/show_bug.cgi?id=10633

[14] [whatwg] Timeouts and monotonic vs clock time ( ( 版)) http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2011-January/029734.html

[15] [whatwg] setTimeout clamps ( 版) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-January/029886.html

[16] Web Applications 1.0 r5879 Define 'this' for setTimeout functions ( ( 版)) http://html5.org/tools/web-apps-tracker?from=5878&to=5879

[17] Web Applications 1.0 r6492 Explain ordering of setTimeout() calls better ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6491&to=6492

[18] Web Applications 1.0 r6899 Make setTimeout() not be well-ordered across multiple browsing contexts, so as to allow user agents to e.g. throttle setTimeout()s in background tabs. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6898&to=6899

[19] Web Applications 1.0 r6949 Turns out clearTimeout() and clearInterval() are synonyms! Who knew? Oh what a tangled Web we weave.Fixing https://www.w3.org/Bugs/Public/show_bug.cgi?id=14907 ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6948&to=6949

[20] IRC logs: freenode / #whatwg / 20120626 ( ( 版)) http://krijnhoetmer.nl/irc-logs/whatwg/20120626#l-1008

[21] Window Object 1.0 ( ( 版)) http://www.w3.org/TR/Window/#timers

[22] Bug 15007 – Add an API to queue a task ( ( 版)) https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007

[23] Web Applications 1.0 r7737 Try to more closely match reality. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=7736&to=7737

[24] Web Applications 1.0 r8095 Refactor the timer code to match current practice in WebKit, Gecko, and Blink (the ones whose source I could examine) ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8094&to=8095

[25] Bug 376643 – setInterval fires repeatedly with incorrectly small delays if machine is suspend/resumed, hibernate/resumed or process is SIGSTOP/SIGCONT (2007-06-23 10:13:26 +09:00 版) https://bugzilla.mozilla.org/show_bug.cgi?id=376643

[26] HTML5 IRC logs: freenode / #whatwg / 20070622 ( 版) http://krijnhoetmer.nl/irc-logs/whatwg/20070622#l-608 (名無しさん 2007-06-23 01:21:49 +00:00)

[27] Web Applications 1.0 r6362 Make setInterval() clamp at 4ms as well. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=6361&to=6362

[34] Efficient Script Yielding ( 版) http://w3c-test.org/webperf/specs/setImmediate/

[1] Scope of setTimeout Method http://www2u.biglobe.ne.jp/~oz-07ams/prog/js-notes/setTimeout.html

[7] for 文を setTimeout に変換する - IT戦記 ( 版) http://d.hatena.ne.jp/amachang/20071108/1194501306

[8] JSDeferred を高速化する (試し中) - 冬通りに消え行く制服ガールは、夢物語にリアルを求めない。 - subtech ( 版) http://subtech.g.hatena.ne.jp/cho45/20090125/1232831437

[28] zarame.com/zide/ 日記(2005-11-24) http://zarame.com/zide/diary/20051124.html#p01 (名無しさん 2005-12-07 11:45:55 +00:00)

[29] ぷろじぇくと、みすじら。 http://www.fastwave.gr.jp/diarysrv/misuzilla/200511c.html#20051126-4 (名無しさん 2005-12-07 11:46:42 +00:00)

[30] zarame.com/zide/ 日記(2005-11-28) http://zarame.com/zide/diary/20051128.html#p01 (名無しさん 2005-12-07 11:46:56 +00:00)

[35] setIntervalとsetTimeoutを調べた結果余分なことになった - 三等兵 ( ( 版)) http://d.hatena.ne.jp/sandai/20100513/p1

[36] window.setTimeout - Web API reference | MDN ( ( 版)) https://developer.mozilla.org/en/docs/Web/API/window.setTimeout

[37] Issue 792 - chromium - TRACKING: Chrome does not implement the Webkit 10ms setTimeout clamp. - An open-source project to help move the web forward. - Google Project Hosting ( ( 版)) https://code.google.com/p/chromium/issues/detail?id=792

[38] David Baron's weblog: setTimeout with a shorter delay ( ( 版)) http://dbaron.org/log/20100309-faster-timeouts

[39] 123273 – setTimeout(something, 0) causes 100% CPU constant ( ( 版)) https://bugzilla.mozilla.org/show_bug.cgi?id=123273

[40] 686201 – implementation: setImmediate API ( ( 版)) https://bugzilla.mozilla.org/show_bug.cgi?id=686201

[41] 839816 – Make next-tick mechanisms like postMessage faster ( ( 版)) https://bugzilla.mozilla.org/show_bug.cgi?id=839816

[42] [whatwg] Proposal: requestBackgroundProcessing() ( ( 版)) http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2014-February/042075.html

[43] Web Applications 1.0 r8535 Move a setTimeout() requirement from prose to IDL. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8534&to=8535

[44] Web Applications 1.0 r8508 Make clearTimeout()/clearInterval() (with no arguments) into no-ops; change the <table> role mapping so that tables aren't interactive. ( ( 版)) http://html5.org/tools/web-apps-tracker?from=8507&to=8508

[3] Web Applications 1.0 r8880 Fix callback logic to reference Web IDL and use the right conventions. ( 版) https://html5.org/r/8880

[5] Fixes #179: Add note about clearing timers · whatwg/html@4b3e06e ( 版) https://github.com/whatwg/html/commit/4b3e06eaa750f129f25999c742dfc782daa085c9

[11] Update ECMAScript and IDL integration · whatwg/html@550c57f ( 版) https://github.com/whatwg/html/commit/550c57f186dd73784a8e4660440db2f642e37c0e

[61] Rewrite script execution on top of ES · whatwg/html@4891d18 ( 版) https://github.com/whatwg/html/commit/4891d18aaf2df1d40aa61f467a5a10cfc19dd85d

[64] Add <script type="module"> and module resolution/fetching/evaluation · whatwg/html@cd1a9fb ( 版) https://github.com/whatwg/html/commit/cd1a9fb1e83f7d0bc30be8b34ecdaf444a0b19a4

[67] Editorial: put properties shared across globals on mixin · whatwg/html@cdd48e1 ( 版) https://github.com/whatwg/html/commit/cdd48e1f570c817402bf62108847c4a9f4b00b1e

[68] Wire up HostEnsureCanCompileStrings to CSP · whatwg/html@374b54d ( 版) https://github.com/whatwg/html/commit/374b54d3fce7c9c72b940d3e97f8e8f2845cde9b

[121] Purpose of timer nesting level for repeating timers is unclear · Issue #239 · whatwg/html ( ()) https://github.com/whatwg/html/issues/239

[122] Existing Intervention to add--setTimeout/setInterval are aligned/clamped to 1 second in many browsers · Issue #5 · WICG/interventions () https://github.com/WICG/interventions/issues/5

[124] Background Tabs in Chrome 57  |  Web  |  Google Developers () https://developers.google.com/web/updates/2017/03/background_tabs

[125] Solve mobile performance (domenic著, ) https://github.com/whatwg/html/commit/d5f415efc7216aa5f527f4d94262aef2fffed8f6

[126] Revert "Solve mobile performance" (domenic著, ) https://github.com/whatwg/html/commit/3716990e05b51850ef7d3c874142b83fe9dd0aa6

[127] Clarify what should actually happen to negative timeouts (zcorpan著, ) https://github.com/whatwg/html/commit/f35e8b70886b5d9594a3d7869936470ea9493c43

[131] Editorial: move base URL from "module script" to "script" (domenic著, ) https://github.com/whatwg/html/commit/5458513792ab00d58e6c91ba48faaa611d034a2e

[132] Make integrity="" work on module scripts (domenic著, ) https://github.com/whatwg/html/commit/9275d955dcd604e959cfcc672e0c234b1b8c00db

[133] Fix import() inside setTimeout()/setInterval() strings (domenic著, ) https://github.com/whatwg/html/commit/12cdfefad607fc824e6ea64a4b967fc24043f684

[138] setTimeout() base URL/script fetch options behavior seems wrong · Issue #3116 · whatwg/html () https://github.com/whatwg/html/issues/3116

[139] Fix import() inside setTimeout()/setInterval() strings by domenic · Pull Request #3117 · whatwg/html () https://github.com/whatwg/html/pull/3117

[140] Chrome’s Headless mode gets an upgrade: introducing `--headless=new` - Chrome Developers (, ) https://developer.chrome.com/articles/new-headless/#-virtual-time-budget