Skip to content

Instantly share code, notes, and snippets.

@matschaffer
Created January 16, 2012 17:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matschaffer/1622014 to your computer and use it in GitHub Desktop.
Save matschaffer/1622014 to your computer and use it in GitHub Desktop.
A text field monitoring plugin for jQuery
(function($) {
return $.fn.monitor = function(fn) {
var changed, currentVal, cycle, lastVal, timeout,
_this = this;
currentVal = this.val();
lastVal = currentVal;
timeout = null;
changed = function() {
return currentVal !== lastVal;
};
cycle = function() {
currentVal = _this.val();
if (changed()) fn(currentVal);
lastVal = currentVal;
return timeout = setTimeout(cycle, 1000);
};
this.keyup(function() {
clearTimeout(timeout);
return cycle();
});
return cycle();
};
})(jQuery);
(($) ->
$.fn.monitor = (fn) ->
currentVal = @val()
lastVal = currentVal
timeout = null
changed = () =>
currentVal != lastVal
cycle = () =>
currentVal = @val()
fn(currentVal) if (changed())
lastVal = currentVal
timeout = setTimeout cycle, 1000
@keyup () ->
clearTimeout(timeout)
cycle()
cycle()
)(jQuery)
@beku8
Copy link

beku8 commented Jan 30, 2013

can you provide the sample code using this plugin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment