A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date/now below:

Date.now() - JavaScript | MDN

Date.now()

Baseline Widely available

Date.now() は静的メソッドで、 UTC (協定世界時) で 1970 年 1 月 1 日の夜半と定義されている元期からの経過時間を、ミリ秒単位で返します。

試してみましょう
// この例は実行に 2 秒掛かります
const start = Date.now();

console.log("starting timer...");
// 予想される結果: "starting timer..."

setTimeout(() => {
  const ms = Date.now() - start;

  console.log(`seconds elapsed = ${Math.floor(ms / 1000)}`);
  // 予想される結果: "seconds elapsed = 2"
}, 2000);
構文 引数

なし。

返値

現在時刻のタイムスタンプをミリ秒単位で表す数値です。

解説 時間の精度の低下

タイミング攻撃やフィンガープリンティングに対する保護機能を提供するために、 someFile.lastModified の精度がブラウザーの設定に応じて丸められることがあります。 Firefox では、privacy.reduceTimerPrecision 設定は既定で有効になっており、既定で 2 ミリ秒になります。この場合、精度は 100ms または privacy.resistFingerprinting.reduceTimerPrecision.microseconds の値のどちらか大きい方になります。

例えば、時刻の精度を下げた場合、 Date.now() の結果は常に 2 の倍数になり、privacy.resistFingerprinting を有効にした場合は 100 の倍数(または privacy.resistFingerprinting.reduceTimerPrecision.microseconds)になります。

// Firefox 60 での時間の制度の低下 (2ms)
Date.now();
// 取りうる値:
// 1519211809934
// 1519211810362
// 1519211811670
// …

// `privacy.resistFingerprinting` が有効な場合の時間の制度の低下
Date.now();
// 取りうる値:
// 1519129853500
// 1519129858900
// 1519129864400
// …
例 経過時間の測定

Date.now() を使用して現在の時刻をミリ秒単位で取得し、前回の時刻を差し引くと、2 つの呼び出し間の経過時間を求めることができます。

const start = Date.now();
doSomeLongRunningProcess();
console.log(`経過時間: ${Date.now() - start} ms`);

より複雑なシナリオでは、パフォーマンス API を使用することをお勧めします。

仕様書 ブラウザーの互換性 関連情報

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4