我正在以这种方式编写代码,但我希望在循环中完成它。我想要充满活力。我该怎么办?
$("#sound-player").attr("src",secilen[0]);
var x = document.getElementById("sound-player");
x.play();
x.onended = function() {
$("#sound-player").attr("src",secilen[1]);
var x = document.getElementById("sound-player");
x.play();
x.onended = function() {
$("#sound-player").attr("src",secilen[2]);
var x = document.getElementById("sound-player");
x.play();
x.onended = function() {
$("#sound-player").attr("src",secilen[3]);
var x = document.getElementById("sound-player");
x.play();
x.onended = function() {
alert("bitti");
}
}
}
}
答案 0 :(得分:1)
您可以使用递归:
parseDates()
答案 1 :(得分:-1)
重复使用变量x
,而不是重新定义它。
var player = document.getElementById('sound-player');
var i = 0;
function playSource(src){
// change the src attribute and play.
if (secilen[scr] !== undefined) {
player.setAttribute('src', secilen[src]);
player.play();
}
else {
alert('bitti')
}
}
player.onended = function(){
i++;
playSource(i);
}
playSource(i); //start playing the first sound