I have the following scenario:
when a user stops typing in the text area, I want to wait for 2 seconds and if the user didn't change anything in the textarea within those 2 seconds, I want to save the content of the textarea to the server. If the user changes something in the textarea within those 2 seconds, I want to restart the wait timeout.
In JavaScript, I would implement it something like this
http://codepen.io/ondrejsevcik/pen/LRxWQP
// Html
<textarea id="textarea"></textarea>
<pre id="server"></pre>
// JavaScript
var textarea = document.querySelector('#textarea');
var textValue = "";
textarea.oninput = function (e) {
textValue = e.target.value;
setSaveTimeout();
}
let saveTimeout;
function setSaveTimeout() {
if (saveTimeout) {
clearTimeout(saveTimeout);
}
saveTimeout = setTimeout(saveToServer, 2000);
}
function saveToServer() {
document.querySelector('#server').innerText =
'Value saved to server: ' + textValue;
}