gpt4 book ai didi

javascript - 从addEventListener()调用函数

转载 作者:行者123 更新时间:2023-12-03 02:20:34 25 4
gpt4 key购买 nike

我有下一个代码,我无法调用ShowMedalMessage()函数

var totalsounds = 3;
var currentsound = 1;
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'notify.wav');
audioElement.setAttribute('autoplay', 'autoplay');
audioElement.addEventListener('ended', function() {
if (currentsound < totalsounds) {
this.currentTime = 0;
this.play();
currentsound = currentsound + 1;
}
ShowMedalMessage(1);
}, false);​

如果对该函数的调用在audioElement.addEventListener之前,则它将正确调用,但如果行ShowMedalMessage(1);在里面,它不起作用:(

谢谢!

最佳答案

这在Chrome和Firefox中对我有用。

Hese是现场example

function showMessage() {
document.write("Sound played");
}

var totalsounds = 3;
var currentsound = 1;
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'http://www.wav-sounds.com/cartoon/bugsbunny1.wav');
audioElement.setAttribute('autoplay', 'autoplay');
audioElement.addEventListener('ended', function() {
if (currentsound < totalsounds) {
this.currentTime = 0;
this.play();
currentsound = currentsound + 1;
}
showMessage();
}, false);

document.body.appendChild(audioElement);

关于javascript - 从addEventListener()调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13611812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com