I want to listen input field and run processing input value to output value after 3 seconds after finnishing of inputting value.
<html>
<head>
<meta charset="utf-8">
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$('.input').on('input', function() {
var value = $(this).val();
console.log('Triggered input: ' + value);
setTimeout(function() {
console.log('Changing output to: ' + value);
$('.output').text(value); // example operation
}, 3000);
});
});
</script>
</head>
<body>
<input type="text" class="input" />
<hr>
<div class="output">...</div>
</body>
</html>
But the code above will process every single character, instead of expected full string.
In the other words. I want to type "abc", and this value should be processed only one time after delay as "abc", instead of, as now as, as "a", then "ab", then "abc".
How to fix that?
inputevent fires on every keypress. You need to debounce the timeout (which the second part of the accepted answer shows)