2012-11-28 56 views
0

我有一个代码,我不能叫ShowMedalMessage()函数呼叫从使用addEventListener()

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);就在里面,它不工作:(

谢谢!

+0

你检查控制台日志在Chrome/Firefox的错误?我没有看到任何错误的代码。 –

+0

它可能不会在事件发生时启动,它会记录一些错误? –

+0

我看,在Firefox不工作的音频播放...与o r没有ShowMedalMessage()...怪异的:/ – MGE

回答

0

这个工作对我来说在Chrome和Firefox中。

是灰褐色现场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); 
+0

不能在firefox中工作..我不知道为什么,我无法访问ShowMedalMessage从其他功能...怪异的。 – MGE

+0

现场示例是否适合您?如果它在Firefox中不起作用,请尝试更新浏览器。 – AlexandruSerban

+0

你的例子正在工作,但不是我的firefox http://bit.ly/U2YrdM,正如你所看到的,该功能并没有在任何浏览器中调用 – MGE