hi im working with lodash .. everything working so good .. but i have problem thats i want debouce not working when enter key is pressed .. and this is my lodash key ..
search_products:_.debounce(function(event)
{
// my code here
// how can i let debounce work with all keys but not with enter key
},5),
i want the delay 5ml not working with enter also if there is any ather method like debouce or any anther library can anyone help me here is that possable thanks
You can extract the debounced function and only call it when something other than the enter key is pressed.
{
search_products: function (event) {
if (event.code !== 'Enter') {
debounced(event);
}
}
}
const debounced = _.debounce(function(event) {
// ...
}, 5);
You can use the flush() method included with lodash debounce
flush will immediately invoke all delayed function invocations.
Type below and there will be a 1 second delay, but hit enter and it will log the result immediately.
const debounced = _.debounce(value => {
console.log(value);
}, 1000);
const handler = event => {
if (event.key === 'Enter') return debounced.flush();
debounced(event.target.value);
}
document.querySelector('input').addEventListener('keyup', handler);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<input type="text" placeholder="type something">