I am attempting to play sounds in sequence using setTimeout
. In my array named challenge
, I have (for testing purposes) the values [0,1,2,3]
. Additionally, I have a function called play(n, true)
for starting the sound and play(n, false)
for stopping the sound. My goal is to:
play(0, true)
.wait1seconds.play(0,false)
.wait1secondplay(1, true)
.wait1second.play(1,false)
.wait1second and so on.
This is what I have attempted up to this point:
watch: {
turn: function(turn) {
if (this.turn === 0) {
var self = this;
var time = 500;
var timeArray = [[],[]]; // I tried with and without this array
for (var i = 0; i < this.challenge.length; i ++) {
timeArray[0][i] = setTimeout(function() {
self.play(i, true);
}, time);
timeArray[1][i] = setTimeout(function() {
self.play(i, false);
this.time += 1500; // I tried with and without this
}, time + 1000);
}
}
}
}
When I omit the array, all the sounds play simultaneously and occasionally produce the desired sound. However, utilizing the array results in an object error.