samedi 28 février 2015

Debounce a function with argument


I'm trying to debounce a save function that takes the object to be saved as a parameter. Something like:



var save = _.debounce(function(obj) {
...
}, delay);


For an auto-save that fires on keystroke, this stops the save from happening until the user stops typing, or at least that's the idea.


Where this falls apart is if I try to save two objects in quick succession. Because the debounce doesn't take the passed in object into account, only the second call to save will fire and only one object will be saved.


I could make obj an instance of a class that has its own save method that takes care of debouncing saves to just that object. Or keep a list of partial/curried functions somewhere, but I'm hoping there's a one stop solution out there. Something like:



var save = _.easySolution(function(obj) {
...
}, delay);




Aucun commentaire:

Enregistrer un commentaire