Skip to content

Instantly share code, notes, and snippets.

@drewlesueur
Created May 26, 2010 02:34
Show Gist options
  • Save drewlesueur/413982 to your computer and use it in GitHub Desktop.
Save drewlesueur/413982 to your computer and use it in GitHub Desktop.
Autosave jQuery plugin
// 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