【发布时间】:2022-01-20 21:59:14
【问题描述】:
我正在尝试使用一个按钮来暂停音频元素,该功能除了alert() 功能外工作正常。警报只是弹出并永远停留在网站的顶部。
这是我的 HTML 代码
<button id="threeSecTimer">3s</button>
<figcaption>Fire</figcaption>
<audio controls loop>
<source src="xxx" type="audio/mpeg" />
Your Browser doesn't support the audio
</audio>
<figcaption>Forest</figcaption>
<audio controls loop>
<source src="xxx" type="audio/wav" />
Your Browser doesn't support the audio
</audio>
这里是js
var threeSecTimer = document.getElementById("threeSecTimer");
var allMusic = document.getElementsByTagName("audio");
function pause3s(e) {
setTimeout(function () {
for (var i = 0; i < allMusic.length; i++) {
if ((allMusic[i] = e.target)) {
allMusic[i].pause();
allMusic[i].currentTime = 0;
alert("time is up!");
}
}
}, 3000);
}
threeSecTimer.addEventListener("click", pause3s);```
【问题讨论】:
-
alert 暂停执行直到用户点击它,这是正常行为
-
并请缩进您的代码,这将有助于(您和其他人)阅读它
-
@Cid 感谢您的建议。将来会缩进我的代码。
-
@Cid 我点击了警报中的确定按钮,警报仍然存在
标签: javascript loops alert