Skip to content

Instantly share code, notes, and snippets.

@melz
Created June 18, 2013 17:41
Show Gist options
  • Save melz/5807594 to your computer and use it in GitHub Desktop.
Save melz/5807594 to your computer and use it in GitHub Desktop.
jQuery: Save responses when the user has finished typing.
// For each input element, we will assign them a timer to avoid sending too many changes to the server at the same time.
var timeout = [];
$('.input-element').each(function() {
timeout[this.id] = null;
})
$('.input-textarea').on('input', function() {
clearTimeout(timeout[this.id]);
var response = $(this).val();
var question_id = $(this).attr("rel");
timeout[this.id] = setTimeout(function () {
if (response.length) {
$.post('/save',
{
"response": response,
"question_id": question_id
},
function(data) {
window.console && console.log(data);
}
);
}
}, 800);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment