T
T
tehnazavr2018-02-14 13:15:13
JavaScript
tehnazavr, 2018-02-14 13:15:13

How to send a search request after a certain time after input?

You need to do a search. I thought to do it on the oninput event, but then too many requests would go to the server. And the onchange event is fired when the focus changes. How to make the request be sent after some time after the user has stopped typing, let's say after 2 seconds?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Petrov, 2018-02-14
@tehnazavr

var timer; // создаем переменную для таймера
$('input').keyup(function(){
  cleatInterval(timer); //если человек написал символ, то удаляем таймер, который создаем далее.
  timer=setTimeout(function(){ //создаем таймер, который сработает, если в течение двух секунд его не удалили
    //ajax-запрос 
  },2000);
});

Code from the head, did not test it. But the idea is like this.

N
Nikita, 2018-02-14
@Rema1ns

debounce

P
Philip Gaponenko, 2018-02-14
@filgaponenko

https://learn.javascript.ru/task/debounce

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question