SVGSVGElement: getCurrentTime() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

SVGSVGElement 接口的 getCurrentTime() 方法返回当前 SVG 文档片段相对于其开始时间的时间(以秒为单位)。

如果在文档时间线开始之前调用 getCurrentTime()(例如,在文档的 SVGLoad 事件触发之前,在 <script> 元素中运行的脚本),则返回 0

语法

js
getCurrentTime()

参数

无。

返回值

浮点数。

示例

获取当前时间

html
<svg
  id="exampleSVG"
  width="200"
  height="200"
  xmlns="http://www.w3.org/2000/svg">
  <circle id="circle1" cx="100" cy="100" r="50" fill="blue" />
</svg>
<button id="getTimeButton">Get Current Time</button>
<p id="currentTimeDisplay"></p>
js
const svgElement = document.getElementById("exampleSVG");
const getTimeButton = document.getElementById("getTimeButton");
const currentTimeDisplay = document.getElementById("currentTimeDisplay");

getTimeButton.addEventListener("click", () => {
  const currentTime = svgElement.getCurrentTime();
  currentTimeDisplay.textContent = `Current time in the SVG: ${currentTime} seconds`;
});

规范

规范
SVG 动画级别 2
# __svg__SVGSVGElement__getCurrentTime

浏览器兼容性