I'm working on an application that plays multiple sounds controlled by the user. I am trying to adjust the volume of the sounds, which can be done independently. I added a timeout function to the keyup, so it will adjust the volume after 1.5 seconds of no input.
The problem is all the boxes use the same timer, so if you edit multiple within the 1.5 seconds, only the last one gets updated.
I tried to remedy this by using an array to keep track of the separate functions, but realized it wasn't copying the function, just saving a reference to it.
I can't explicitly create variables for each sound as I will let users add their own. Any ideas on how to solve this problem.
I've simplified it here: http://codepen.io/samkeddy/pen/VLKJjJ
//timer
volDelay = [];
delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$('body').on('keyup', '.volume', function () {
soundID = $(this).parent().data('soundid');
newVol = $(this).val();
volDelay[soundID] = delay;
volDelay[soundID](function(){
console.log('set vol on ' +soundID+ ' to ' +newVol);
//set volume
$('.sound-'+soundID).html(newVol);
}, 1500 );
});