Skip to content Skip to sidebar Skip to footer

Call A Function From Addeventlistener()

I have the next code and I can't call ShowMedalMessage() function var totalsounds = 3; var currentsound = 1; var audioElement = document.createElement('audio'); audioElement.setAtt

Solution 1:

this worked for me in chrome and in firefox.

Hese is the live example

functionshowMessage() {
  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);

Post a Comment for "Call A Function From Addeventlistener()"