I want to send keystrokes in a text box, but only after 1 s.
this will reduce the burden of sending an ajax request for every keystroke.
for example. if the user enters 4 letters in one second, the request will send all these 4 letters. and allows you to say that it enters non-stop after 1 minute, then the script will send at 1-second intervals until it stops typing.
currently my script is as follows:
$("#type_post").live('keyup', function() {
$.post('posts.php', {post: $("#type_post").val()});
});
but it sends out all keystrokes.
Can anyone help me here.
UPDATE: here is the code I used.
var last_post = '';
$('#type_post').focus(function() {
setInterval(function() {
if($('#type_post').val() != last_post)
{
$.post('posts.php', {post: $("#type_post").val()});
last_post = $("#type_post").val();
}
}, 1000);
});
source
share