I'm studying how to pass arguments into a setTimeout (as advised here.) In the context of a while-loop which activates an HTML5 Audio element.
This works:
window.setTimeout(playTone, time, phrasePitches[x]);
Curiously, this does not work:
window.setTimeout(function(){
playTone(phrasePitches[x]);
}, time);
In the console, the timeouts occur as scheduled, each displaying:
TypeError: Cannot set property 'currentTime' of null
So for some reason, the second method does not want to accept the array... any idea what's going on here?
EDIT... the full code:
function playTone(tone){
var tone = document.getElementById(tone);
tone.currentTime = 0;
tone.play();
};
var pitchSet = new Array ("C3","D3","E3","F3","G3","A3","B3","C4");
fyshuffle (pitchSet); // The Fischer-Yater shuffle function
var i = 0;
var phrasePitches = new Array();
while (i < 4) {
phrasePitches.push(pitchSet[i]);
i++;
}
var x=0;
var time = 0;
while(x<4){
// window.setTimeout(playTone, time, phrasePitches[x]); // This works.
window.setTimeout(function(){
playTone(phrasePitches[x]);
}, time);
time += 2000;
x++;
}