var myfiles=["1.mp3","2.mp3","3.mp3","4.mp3","5.mp3"]; audio.onended= function (){ var len = myfiles.length; if(len){ len *= Math.random(); len |= 0; len = "audio/"+myfiles.splice(len,1)[0] this.src=len; this.play(); } };