What jQuery event do I use to query the database?

2
jQuery(function($){

    // Chamando as funções
    $('#busca-cidades').keyup(function(){ ...

I am doing a query in the database and returning this query formatted with html / css and the query is in WordPress SQL. So far nothing very different. The problem is that when I query with the keyup event every letter I type makes a query and in addition, the page still blinks as if it had given refresh .

What event could I use to make the query only when I finish typing the word?

    
asked by anonymous 13.07.2015 / 00:25

2 answers

2

You will always have to guess when the user has just typed. for this we will assume 5 seconds.

var typingTimer;                
var doneTypingInterval = 5000; // 5 segundos 


$('#busca-cidades').keyup(function(){
clearTimeout(typingTimer);
if ($('#busca-cidades').val) {
    typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
});

function doneTyping () {
    //do something
}
    
13.07.2015 / 00:30
1

You will need to do a query ajax and fetch the data through a server side file, then read through the success function where you will have access to the data coming from the php / asp file by reading the same in json.

    
31.07.2015 / 15:23