Created
May 26, 2010 02:34
-
-
Save drewlesueur/413982 to your computer and use it in GitHub Desktop.
Autosave jQuery plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery plugin to easily implement autosave | |
// | |
// | |
// | |
// | |
;(function($) { | |
$.fn.typed = function(settings) { | |
var config = { | |
'callback': function(){}, | |
wait : 750 | |
}; | |
if (settings) $.extend(config, settings); | |
this.each(function(){ | |
$(this).attr('old-val', $(this).val()); | |
var that = this | |
function save(){ | |
var val = $(that).val() | |
var old_val = $(that).attr('old-val'); | |
if (val != old_val) { | |
config.callback.call(that) | |
$(that).attr('old-val', val ) | |
} | |
} | |
var t; | |
$(this).keydown(function(){ | |
clearTimeout(t) | |
}) | |
$(this).keyup(function(){ | |
clearTimeout(t) | |
t = setTimeout(save, config.wait); | |
}) | |
}) | |
return this; | |
}; | |
})(jQuery); | |
//example usage | |
// $('.datepicker').typed({callback : change_form}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment