Search code examples
javascriptjqueryhtmlwysiwyg

Better way to fire save auto draft to database base on Textarea


Can anyone give a better way to fire save_autodraft()? save_autodraft() function will save textarea value into my database base on Textarea input throught ajax.

onchange seems like a little bit not really efficient way and a little bit costly my server because user usually typing (make changes) every second and typing hundred or thousand characters on it. setTimeout?


Solution

  • Do both - throttle the function

    var timeout;
    element.on('input',function() {
       clearTimeout(timeout);
       timeout = setTimeout(function() {
           //save
       },1000);
    });