jquery - Best way to keep textarea synced with server text file -
hi i'm making simple application users can type information text area , not have worry saving or losing it. i'm using jquery/javascript detect changes text area, using ajax send value php writing server. have basic "busy" lock prevent overlapping ajax. however, i'm finding unreliable. there better way this?
var busy = false; function save () { if(busy === false) { busy = true; var content = $('#text').val(); var file = 'store.txt'; $.get("storecontent.php", { content_fromjq: content, file_fromjq: file}) .done(function(data) { busy = false; }); } } $('#text').bind('input propertychange', function() { save(); });
php:
if (isset($_get['content_fromjq'])) { $content = $_get['content_fromjq']; $file = $_get['file_fromjq']; $html = new domdocument(); $html->loadhtmlfile($file); $html->getelementbyid('content')->nodevalue = $content; $html->savehtmlfile($file); }
here : http://jsfiddle.net/z4rjq/
save listener when text change call function :
function checksave(){ cleartimeout(timer); timer = settimeout(save, 5000); }
this function clear timer , set other one.
in other word, there no save until user didnt press key 5 sec (adjustable).
that allow not send thousand of ajax save send 1 after finished typing something.
Comments
Post a Comment