diff --git a/files/zh-cn/web/api/htmlmediaelement/currenttime/index.md b/files/zh-cn/web/api/htmlmediaelement/currenttime/index.md index 2d3d13bdd95ea0..de907836f82cdb 100644 --- a/files/zh-cn/web/api/htmlmediaelement/currenttime/index.md +++ b/files/zh-cn/web/api/htmlmediaelement/currenttime/index.md @@ -1,29 +1,59 @@ --- -title: HTMLMediaElement.currentTime +title: HTMLMediaElement:currentTime 属性 slug: Web/API/HTMLMediaElement/currentTime +l10n: + sourceCommit: 3b5a1c0dfd59257c0a51052a9efa7b0108f8ecca --- -{{APIRef("HTML DOM")}}**`HTMLMediaElement.currentTime`** 属性会以秒为单位返回当前媒体元素的播放时间。设置这个属性会改变媒体元素当前播放位置。 +{{APIRef("HTML DOM")}} -> [!NOTE] -> 该处原文:The **`HTMLMediaElement.currentTime`** property gives the current playback time in seconds. Setting this value seeks the media to the new time. +{{domxref("HTMLMediaElement")}} 接口的 **`currentTime`** 属性指定当前的播放时间(以秒为单位)。 -## 语法 +改变 `currentTime` 的值会将媒体定位(seek)到新的时间。 + +## 值 + +一个双精度浮点数,表示当前的播放时间(以秒为单位)。 + +如果媒体尚未播放,`currentTime` 的值表示一旦调用 {{domxref("HTMLMediaElement.play", "play()")}} 方法,媒体将开始播放的时间点。 + +将 `currentTime` 设置为新值会将媒体定位到给定的时间(如果媒体可用)。 + +对于没有已知持续时间的媒体(例如实时流式传输的媒体),浏览器可能无法从媒体缓冲区中获取已过期的媒体部分。此外,对于时间线不从 0 秒开始的媒体,无法定位到其时间线最早的时间点之前的时间。 + +可以使用 {{domxref("HTMLMediaElement.duration", "duration")}} 属性确定媒体的长度(以秒为单位)。 + +## 示例 ```js -var cTime = video.currentTime; -video.currentTime = 35; +const video = document.createElement("video"); +console.log(video.currentTime); ``` -### 返回值 +## 使用说明 -一个 `double` 双精度浮点数。 +### 降低时间精度 -## 示例 +为了防止计时攻击和[指纹识别](/zh-CN/docs/Glossary/Fingerprinting),`video.currentTime` 的精度可能会根据浏览器设置进行舍入。在 Firefox 中,`privacy.reduceTimerPrecision` 首选项被默认启用且默认为 2ms。你还可以启用 `privacy.resistFingerprinting`,在这种情况下,精度将为 100ms 和 `privacy.resistFingerprinting.reduceTimerPrecision.microseconds` 的值中的较大值。 + +例如,在降低了时间精度的情况下,`video.currentTime` 的结果总是 0.002 的倍数,或者在 `privacy.resistFingerprinting` 启用的情况下,为 0.1(或 `privacy.resistFingerprinting.reduceTimerPrecision.microseconds`)的倍数。 ```js -var obj = document.createElement("video"); -console.log(obj.currentTime); +// Firefox 60 中降低的时间精度(2ms) +video.currentTime; +// 可以是: +// 23.404 +// 24.192 +// 25.514 +// …… + +// `privacy.resistFingerprinting` 启用的情况下的降低的时间精度 +video.currentTime; +// 可以是: +// 49.8 +// 50.6 +// 51.7 +// …… ``` ## 规范 @@ -34,7 +64,8 @@ console.log(obj.currentTime); {{Compat}} -## 相关链接 +## 参见 -- The interface defining it, {{domxref("HTMLMediaElement")}}. -- [`HTMLMediaElement.fastSeek()`](/zh-CN/docs/Web/API/HTMLMediaElement/fastSeek) for another method of setting the time +- {{domxref("HTMLMediaElement")}}:用于定义 `HTMLMediaElement.currentTime` 属性的接口 +- {{domxref("HTMLMediaElement.fastSeek()")}}:另一种设置时间的方法 +- {{domxref("HTMLMediaElement.duration")}}:媒体的时长(以秒为单位)