我有下一个代码,我无法调用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);