console: timeLog() 靜態方法

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

注意:此功能在 Web Workers 中可用。

console.timeLog() 靜態方法用於記錄先前透過呼叫 console.time() 啟動的計時器的當前值。

語法

js
console.timeLog()
console.timeLog(label)
console.timeLog(label, val1)
console.timeLog(label, val1, /* …, */ valN)

引數

label 可選

記錄到控制檯的計時器名稱。如果省略此引數,則使用標籤 "default"。

valN 可選

計時器輸出後要記錄到控制檯的其他值。

返回值

無(undefined)。

描述

console.timeLog() 方法記錄計時器的當前值。

該方法可以傳遞計時器名稱。這將嘗試記錄先前呼叫 console.time() 時建立的具有該名稱的計時器的值。

js
console.time("reticulating splines");
reticulateSplines();
console.timeLog("reticulating splines");
// reticulating splines: 650ms

如果省略計時器名稱,則計時器名為 "default"

js
console.time();
reticulateSplines();
console.timeLog();
// default: 780ms
js
console.time("default");
reticulateSplines();
console.timeLog();
// default: 780ms

如果沒有對應的計時器,console.timeLog() 會記錄一個警告,如下所示:

Timer "timer name" doesn't exist.

您可以記錄計時器輸出後的附加值到控制檯。

js
console.time();
reticulateSplines();
console.timeLog("default", "Hello", "world");
// default: 780ms Hello world

有關更多詳細資訊和示例,請參閱文件中的 計時器

示例

js
console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff…");
console.timeEnd("answer time");

上面示例的輸出顯示了使用者關閉第一個警報框所花費的時間,然後是使用者關閉兩個警報框的總累計時間。

answer time: 2542ms debugger eval code:3:9
answer time: 4161ms - timer ended

請注意,計時器的名稱在呼叫 console.timeLog() 記錄計時器值時會顯示,在停止計時器時再次顯示。此外,console.timeEnd() 的呼叫會附帶額外資訊 "timer ended",以明確表示計時器不再跟蹤時間。

規範

規範
控制檯
# timelog

瀏覽器相容性

另見