В моем коде метод срабатывает несколько раз или задерживается слишком сильно:
Код: Выделить всё
let debounceTimeout;
function dynamicDebounce(fn, input) {
const delay = Math.min(500 + input.length * 20, 2000);
clearTimeout(debounceTimeout);
debounceTimeout = setTimeout(() => {
fn(input);
}, delay);
}
function handleInputChange(input) {
console.log("User input:", input);
}
document.querySelector('input').addEventListener('input', function(event) {
dynamicDebounce(handleInputChange, event.target.value);
});
Подробнее здесь: https://stackoverflow.com/questions/793 ... ased-input
Мобильная версия