Created
June 18, 2013 17:41
-
-
Save melz/5807594 to your computer and use it in GitHub Desktop.
jQuery: Save responses when the user has finished typing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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