public
Last active

A text field monitoring plugin for jQuery

  • Download Gist
jquery.monitor.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(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);
jquery.monitor.js.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(($) ->
$.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)

can you provide the sample code using this plugin?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.