How to stop audio when the last song in the playlist end

Hi guys,
The following code play music on the playlist when click or touch. But the it won’t stop when the last song end. How do I make it stop.
HTML:[code]

  • Song1
  • Song2
[/code] JS:[code] var audio = document.createElement('audio'), source = document.getElementById('source'), path = '../music/', tracks = [ {'track':1,'name':'song1','file':'song1.mp3'}, {'track':2,'name':'song1','file':'song2.mp3'} ], myLis = document.querySelectorAll('#playlist li'), i = myLis.length; while (i--) { (function(x){ myLis[x].addEventListener('click', function() { loadTracks(x); }); })(i); }

var loadTracks = function(url) {
if (audio) audio.remove();

var index = 0;
audio = document.createElement('audio');
source.insertBefore(audio, source.childNodes[0]);
var tag = source.getElementsByTagName('audio'),
	tags = source.getElementsByTagName('audio').length;
if ( tags > 1 ) {
	source.removeChild(tag[1]);
}
audio.src = path + tracks[url].file;
audio.addEventListener('canplay', function(e) {
	audio.play();
}, false);
audio.addEventListener('ended', function(e) {
	audio.removeEventListener('canplay', arguments.callee, false);
	if ((index + 1) < myLis.length) {
		index++;
		loadTracks(index);
	} else {
		return;
	}
}, false);

};
[/code]