我試圖讓音頻 beepAudio 在 for 循環(huán)中播放 3 次,現(xiàn)在只有在沒有錯誤消息時才會發(fā)出蜂鳴聲。我嘗試將音頻放入 if (timer == 0) 語句中三次,如下所示: if (timer == 0) { stopTimer(); document.getElementById("beepAudio").play(); document.getElementById("beepAudio1").play(); document.getElementById("beepAudio2").play();}但這不起作用,是否可以使用 for 循環(huán)來做到這一點?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>
計時器的 for 循環(huán)中聲音未播放 3 次
有只小跳蛙
2023-10-14 15:56:17