javascript - streamline jquery autosave text -


$("textarea").bind("keyup", function () {              var keyname = $(this).attr("data-rkn-keyname");             autosave(keyname, this.value);      });      function autosave(key, value) {         $.ajax({             url: "/admin/savelookup",             type: "post",             data: ("keyname=" + key + "&value=" + value),             success: function (data) {                 if (data) {                     $('#saved-signal').show().fadeout(1500);                 }             }         });     } 

i have few textareas on page , i'd them autosave user input. data being edited set of key/value pairs, textarea being value , textarea's 'data-rkn-keyname' attribute being key.

so above code kinda works if user types fast won't catch last character or two. altered so:

$("textarea").bind("keydown", function () {     var keyname = $(this).attr("data-rkn-keyname");     var keyvalue = $(this).value;         settimeout(function (keyname, keyvalue) {             autosave(keyname, keyvalue);         }, 2000);     }); 

so, adding timer breaks it, autosave called have feeling vars losing values.....possibly variable scope. minor issue can work out...more importantly (and purpose post), question: instantiating new timer object , ajax call (=database hit) every time key pressed? or settimeout reset same object if call repeatedly?

what i'd call autosave function second or 2 after keystroke pressed if no keystrokes pressed during time...in other words, if user pauses 2 seconds...fire save function.

or maybe there's better way autosave?

thank you

thanks

settimeout accepts anonymous function without arguments. when included keyname , keyvalue input arguments, anonymous function receive nil both arguments. remove arguments, , values passed successfully.

$("textarea").bind("keydown", function () {     var keyname = $(this).attr("data-rkn-keyname");     var keyvalue = $(this).value;     settimeout(function () {         autosave(keyname, keyvalue);     }, 2000); }); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -