Which jQuery event to use for database query?

Asked

Viewed 810 times

2

jQuery(function($){

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

I am making 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 consult with the event keyup every letter I type up makes a query and in addition, the page still flashes as if giving refresh.

What event could I use for you to consult only when I finished typing the word?

2 answers

2


You will always have to guess when the user finishes writing. 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
}

1

You will have to make an ajax query 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 them in json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.