I have the following JavaScript Code:
for (var x = 0; x < queue.length; x++) {
var song = new Song().loadJSON(queue[x]);
//Not already loaded
if (loaded.indexOf(song.url) == -1) {
addRow("play", song.url, song.title, song.album, song.artist);
loaded.push(song.url);
songs.push(song);
if (song.duration == 0) {
var audio = $("<audio>");
audio.attr("songid", songs.length - 1);
console.log("Writing:", audio.attr("songid"));
audio.on("durationchange", function () {
var id = audio.attr("songid");
console.log("Reading:", id);
songs[id].duration = audio[0].duration;
});
audio.attr("src", song.url);
}
}
}
This prints out:
Writing: 0
Writing: 1
Writing: 2
(3x) Reading: 2
Which leads me to believe that the songid attribute is being written over in each iteration of the for loop. I am not sure how this is possible because the audio variable is local to each iteration. What am I doing wrong?