JS Audio onended 在音频结束时不触发

JS Audio onended not triggering when audio ends

我正在尝试让一个函数在音频结束时触发。我正在使用此功能来创建声音。

function sound(src,volume,repeat) {
    volume = volume || 1
    repeat = repeat || false;
    this.sound = document.createElement("audio");
    this.sound.volume = volume;
    this.sound.src = src;
    this.sound.setAttribute("preload", "");
    this.sound.setAttribute("class", "gamesound");
    if(repeat)this.sound.setAttribute("loop","");
    this.sound.style.display = "none";
    document.body.appendChild(this.sound);
    this.play = function(){
        this.sound.play();
    }
    this.stop = function(){
        this.sound.pause();
    }
    this.pause = function(){
        this.sound.pause();
    }
    this.onend = function (s){
         this.sound.onended = s;                
    }
    this.load = function(){
        this.sound.load();
    }
    this.currentTime = function (t){
        this.sound.currentTime = t;
    }
}

所以可以这样用

var song = new sound("sound.mp3",0.2,true);
song.ended(function(){
    alert("ended");
})

但是没有触发,我哪里错了。

如果您启用了循环选项,则不会触发该事件,在本例中您正在展示。