This code works to fade and loop an audio element when a button is pressed:
$("#loop").click(function(){
var tone = document.getElementById("testTone");
tone.play();
$("#testTone").animate({volume: 0}, 1900);
setInterval(function(){
tone.currentTime = 0;
tone.volume = 1;
tone.play();
$("#testTone").animate({volume: 0}, 1900);
},2000)
});
However, when I name the function, it calls immediately on page load, without pressing the button.
function loopAudio(target){
var tone = document.getElementById(target);
tone.play();
$("#"+target).animate({volume: 0}, 1900);
setInterval(function(){
tone.currentTime = 0;
tone.volume = 1;
tone.play();
$("#"+target).animate({volume: 0}, 1900);
},2000)
};
$("#loop").click(loopAudio("testTone"));
Why? And what is the right way to call this named function?
EDIT: Since passing the parameter calls the function, what is a good workaround strategy to pass in a parameter?