0

我有下一个代码,但我无法调用 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); 行 在里面,它不起作用:(

谢谢!

4

1 回答 1

0

这在 chrome 和 Firefox 中对我有用。

Hese是活生生的例子

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);
于 2012-11-28T18:53:58.173 回答