我试图让音频 beepAudio 在 for 循环中播放 3 次,现在只有在没有错误消息时才会发出蜂鸣声。我尝试将音频放入 if (timer == 0) 语句中三次,如下所示: if (timer == 0) { stopTimer(); document.getElementById("beepAudio").play(); document.getElementById("beepAudio1").play(); document.getElementById("beepAudio2").play();}但这不起作用,是否可以使用 for 循环来做到这一点?let timerId;function startTimer(duration, display) { var timer = duration, minutes, seconds; timerId = setInterval(function() { if (--timer < 0) { timer = duration; } minutes = parseInt(timer / 60, 10); seconds = parseInt(timer % 60, 10); minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; display.textContent = minutes + ":" + seconds; if (timer == 0){ stopTimer(); for (let step = 0; step < 5; step++) { document.getElementById("beepAudio").play(); } alert('Timer Ended'); } }, 1000);} function resetTimer() { clearInterval(timerId); } function stopTimer() { clearInterval(timerId); }document.getElementById("beepAudio").src = "http://soundbible.com/grab.php?id=1252&type=mp3"; document.getElementById("beepAudio").load(); function start10() { var tenMinutes = 60 * 0.1, display10 = document.querySelector('#time'); startTimer(tenMinutes, display10); };<body><audio id="beepAudio"></audio><button onclick="start10()">Start</button> <div>Registration closes in <span id="time">00:05</span> minutes!</div></body>
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
是否是您想要循环播放的相同音频(编辑暗示它是)..无论如何,您可以使用音频的 onending 事件
也许是这样的:
let beepCount = 0;
document.getElementById("beepAudio").addEventListener("ended", () => {
beepCount++;
if(beepCount < 3){
document.getElementById("beepAudio").play();
}
});
然后,当蜂鸣声响起时:
if (timer == 0) {
stopTimer();
beepCount = 0;
document.getElementById("beepAudio").play();
}
添加回答
举报
0/150
提交
取消