我有这个数组:
var song = ['note1.mp3', 'note2.mp3', 'note3.mp3', 'note4.mp3']
for(i = 0; i < song.length; i++) {
setInterval(function() {
//play song[i]
//or console.log(song[i]);
}, 1000);
}
但这不起作用,有人知道为什么吗?我想播放 note1.mp3,然后播放 note2、note3 和 note4。
谢谢!
最佳答案
您遇到范围问题。所有回调都有一个 i
,您必须为每个 i
创建一个范围。您还必须改变回调。
http://jsfiddle.net/1xy4j3eq/7/
var song = ['a', 'b', 'c', 'd', 'e'];
$.each(song, function (i) {
setTimeout(function () {
$("div").text(song[i]);
}, i * 1000);
});
关于javascript - 使用 setInterval 和 Loop 播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25951039/