I have an object, toKeep
, where a MIDI note is contained in the label property of each object.
I want it to play after a certain delay depending on the number of notes that are received.
The problem now is that the variable pinned_neighbor
stays the same even though it should change.
Is the problem that setTimeout
function doesn't receive the data?
for (var ed = 0; ed < toKeep.length; ed++) {
var pinned_neighbor = toKeep[ed].label
if (pinned_neighbor != undefined) {
setTimeout(function(pinned_neighrbor) {
output.playNote(pinned_neighbor, parseInt(midi.substr(2, 2)));
output.stopNote(pinned_neighbor, parseInt(midi.substr(2, 2)), { time: "+" + Math.floor(timefloor / toKeep.length) });
console.log('playing an edge note ' + pinned_neighbor + ' at ' + timecodea + ' on ' + parseInt(midi.substr(2, 2)));
timecodea = timecodea + Math.floor(timefloor / toKeep.length);
}, timecodea);
}
}
You're not passing pinned_neighbor to setTimeout, and you can't do so the way you've written the code currently (as far as I know).
You'll need to use some sort of closure to retain the value of
pinned_neighbor
. You can use an IIFE or you can create another function that callssetTimeout
.IIFE Approach
Separate Function