Skip to content

Instantly share code, notes, and snippets.

@incik
Last active December 24, 2015 07:39
Show Gist options
  • Save incik/6764827 to your computer and use it in GitHub Desktop.
Save incik/6764827 to your computer and use it in GitHub Desktop.
'onChange' callback delayed until user stops changing value of input
/* Calls callback function if value of given element stops changing
elem - element which's value we're watching
callback - anonymous method or callback fuction name
(delay) - delay before callback
*/
wait_for_it = function(elem, callback, delay) {
var old_val;
if (delay == null) {
delay = 750;
}
old_val = elem.val();
return setTimeout((function() {
if (elem.val() === old_val) {
return callback();
}
}), delay);
};
/* Usage example */
$('input').change(function() {
wait_for_it($(this), function() {
alert('foo!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment