如何在Javascript中的音频标记上创建事件侦听器?
我有一个音频标签在我的页面,这将是几分钟长,我希望它在音频不同的时间执行不同的功能。例如,在特定时间记录某些内容 示例代码如何在Javascript中的音频标记上创建事件侦听器?,javascript,html,audio,Javascript,Html,Audio,我有一个音频标签在我的页面,这将是几分钟长,我希望它在音频不同的时间执行不同的功能。例如,在特定时间记录某些内容 示例代码 document.querySelector("audio").addEventListener("executes every second of the audio", e => { if(audio.time == 60) console.log("audio has reached 60 second
document.querySelector("audio").addEventListener("executes every second of the audio", e => {
if(audio.time == 60) console.log("audio has reached 60 seconds")
})
在音频中的不同时间调用函数。
玩
函数playAudio(){
var audio=document.querySelector(“音频”)
音频播放();
变量计时器=0
setInterval(函数(){
计时器+=100;
如果(时间计数器===1000){
console.log(“第一秒”);
//函数调用
}
如果(时间计数器===3500){
console.log(“三秒半”);
//函数调用
}
}, 100);
}
<!DOCTYPE html>
<html>
<head>
<title>Function calls at different times in the audio.</title>
</head>
<body>
<audio src="filename.mp3"></audio>
<button onclick="playAudio()">Play</button>
<script>
function playAudio() {
var audio = document.querySelector("audio")
audio.play();
var timeCounter = 0
setInterval(function() {
timeCounter += 100;
if (timeCounter === 1000) {
console.log("first second");
//function call
}
if (timeCounter === 3500) {
console.log("three seconds and a half");
//function call
}
}, 100);
}
</script>
</body>
</html>