在本教程中,我们将学习如何使用JavaScript获得Youtube视频的当前时间。
获取当前时间
要获得Youtube视频的当前时间,首先我们需要在JavaScript中使用document.getElementById() 方法访问该元素。
const ytvideo = document.getElementById("video-player");
现在,我们需要调用getCurrentTime() 方法来获得当前时间。
ytvideo.getCurrentTime();
完整的例子。
const ytvideo = document.getElementById("video-player");
ytvideo.getCurrentTime();
getCurrentTime() 方法返回视频开始播放后的经过时间(秒)。
注意:经过的时间意味着结束时间-开始时间