[英]How would I make an event listener on an audio tag in Javascript?
我的頁面中有一個音頻標簽,它會長達幾分鍾,我希望它在音頻的不同時間執行不同的功能。 例如在某個時間記錄某些內容。
示例代碼
document.querySelector("audio").addEventListener("executes every second of the audio", e => {
if(audio.time == 60) console.log("audio has reached 60 seconds")
})
<!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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.