我试图让音频 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>
撒科打诨
相关分类