Real-time search saving server?

Asked

Viewed 71 times

0

Hi, I created a script in jQuery searching the database, based on what the person typed in a field input. With each key the user type, the script makes a new requirement for my API and that’s a problem, since I intend to hire a cheap lodging. So I wanted to know how I can improve the following script so I don’t have to keep asking the server all the time and spending the server resources I plan to hire:

$(document).ready(function() {
    $('#input').keyup(function(event) {
        var t = $('#input').val();
        $.getJSON('api', {word: t}, function(json, textStatus) {
            // Em caso de sucesso, essa função será ativa
        });
    });
});
  • 2

    How many records in the database can the server return? If it’s not too big, you can download them all on the first request and just filter them with Javascript.

  • The site I’m developing is a dynamic content site, IE, will always have new updates, consequently a large database...

  • Then you can set a minimum of characters to start searching in the database and redo it every 2 or 3 new characters.

  • creates a function on click instead of keyup and so the query will only be made when the user finishes typing and click a button, or click enter to move forward.

  • You want to reduce the number of server queries, but you haven’t told the criteria that need to be followed for this. There are several ways to reduce the number of requests, some will suit you very well, others not so well. Remember that if you get to the point of harming user usability to gain performance, you are doing it wrong. If you no longer have to improve (without harming the user) the code, then you have to hire more hardware or look for other technologies. And make no mistake, databases support much more "tranco" than you think.

  • That solution served you @Andersonsantos ?

Show 1 more comment

1 answer

0


Creates a function on click instead of keyup and so the query will only be made when the user finishes typing and click a button, or click enter to advance the search.

$(document).ready(function() {
    $('#botao-pesquisar').click(function(){
        var t = $('#input').val();
        $.getJSON('api', {word: t}, function(json, textStatus) {
            // Em caso de sucesso, essa função será ativa
        });
    })
    $('#input').keypress(function(e){
        if(e.which == 13){ // Tecla Enter primida
            $('#botao-pesquisar').click(); // Dispara o evento click no botão de pesquisa
        }
    });
});

It may not be the most perfect way, but it already considerably reduces the constant query on each primitive key.

Browser other questions tagged

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