Javascript x 秒后暂停播放音频

Javascript pause audio playing after x seconds

我首先定义了音频播放的开始时间和结束时间,均以秒为单位

var a = document.getElementById("audioplayer");
a.addEventListener("progress", function () {
    if ($("#audioplayer")[0].currentTime > Math.round(Number(globalStart) + Number(globalDuration)))  {
        $("#audioplayer")[0].pause();
    }
}, true);

我想在当前时间达到 x 秒时停止播放音频

我该怎么做?

您必须在 timeupdate

添加事件侦听器
a.addEventListener('timeupdate', function () {
    if (this.currentTime > Math.round(Number(globalStart) + Number(globalDuration)) && !this.paused)  {
        this.pause();
    }
});