I'm implementing a shuffle feature for my music player. When the shuffle mode is turned on and a song ends, it chooses a random number from 0 to the number of songs that are available (inclusive). So the first listed song is 0, second is 1, and so on.
The random part is working correctly, however, I have added a little bit of extra code to check if the number/index chosen is the same as the song that is currently playing, so it doesn't play the same song twice (or more) consecutively. To accomplish this I made my function recursive; however, whenever the value is the same, it doesn't call the function again. It simply stops.
function shuffle_song(i)
{
var value = Math.floor(Math.random() * ($('div.search-result:visible').length));
if(i == value)
{
console.log(i);
console.log(value);
shuffle_song(value);
}
else
{
return value;
}
}
I put in some console.log() calls for debugging. They are called successfully in Firebug but the function is not called again. There are no other errors in Firebug either. Am I doing something wrong with recursion, or am I missing something entirely?
Thanks for any help!