0

I have the next code and I can't call ShowMedalMessage() function

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);​

If the call to the function is before to audioElement.addEventListener, it calls properly, but if the line ShowMedalMessage(1); is inside, it doesn't work :(

Thanks!

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
MGE
  • 803
  • 2
  • 12
  • 22

1 Answers1

0

this worked for me in chrome and in firefox.

Hese is the live 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);
AlexandruSerban
  • 312
  • 2
  • 9
  • not working in firefox.. and I don't know why, I can't access to ShowMedalMessage from other function... weird. – MGE Nov 28 '12 at 19:00
  • Did the live example work for you ? If it did not work in firefox try updating the browser I guess. – AlexandruSerban Nov 28 '12 at 19:05
  • your example is working, but not mine in firefox http://bit.ly/U2YrdM and as you can see, the function is not called in any browser – MGE Nov 28 '12 at 19:14
  • You should really fix your javascript errors in your local files maybe after that the script will work. You have one at line 512 in general_scripts.js 'document.mn_search is undefined' – AlexandruSerban Nov 28 '12 at 19:47