javascript - How to only get variable every few seconds and update it -
i have form maintenance employees @ company fill out @ end of day describe did day. fill out on phones sometimes. complained if got call or text loose log had typed up.
so did store form values php session variables via ajax , echo out variable if form reloaded long they're signed in.
this 1 of input fields:
<textarea class="form-control" name="8to10" id="d8to10" placeholder="8 10 am" rows="8"> <?php if(isset($_session['d8to10'])){echo htmlspecialchars($_session['d8to10'], ent_quotes);} ?> </textarea> <span id="saved1"><span>
this corresponding javascript grabs field's value , sends through ajax store session variable.
$('#d8to10').on("keyup", function(){ var d8to10 = $(this).val(); $('#saved1').css('display', 'none'); d8to10 = encodeuricomponent(d8to10); //console.log(d8to10); var ajaxrequest; cleartimeout(ajaxrequest); ajaxrequest = settimeout(function() { $.ajax({ type: 'post', url: 'includes/dailylogcachelog.php', data: 'd8to10=' + d8to10, success: function(r) { $('#saved1').css('display', 'inline'); $('#saved1').html('<p>saved</p>'); } }); }, 3000, d8to10); });
so want , can't figure out how right grab variable every 5 or 10 seconds type , save in time frame. because ajax being called after every letter typed , they're complaining saved message blinking much.
anyone know way this?
something might trick, executes ajax 3 seconds after user stops typing. if want have multiple calls while user types, can remove cleartimeout , abort calls.
var timeout_d8to10; var xhr_d8to10; $('#d8to10').on("keyup", function(){ var $this = $(this); if (timeout_d8to10) { cleartimeout(timeout_d8to10); } if ( xhr_d8to10) { xhr_d8to10.abort(); } timeout_d8to10 = settimeout( function () { var d8to10 = $this.val(); d8to10 = encodeuricomponent(d8to10); $('#saved1').css('display', 'none'); xhr_d8to10 = $.ajax({ type: 'post', url: 'includes/dailylogcachelog.php', data: 'd8to10=' + d8to10, success: function(r) { $('#saved1').css('display', 'inline'); $('#saved1').html('<p>saved</p>'); } }); }, 3000); });
Comments
Post a Comment