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
});
});
});
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.
– Woss
The site I’m developing is a dynamic content site, IE, will always have new updates, consequently a large database...
– Anderson Santos
Then you can set a minimum of characters to start searching in the database and redo it every 2 or 3 new characters.
– Woss
creates a function
on click
instead ofkeyup
and so the query will only be made when the user finishes typing and click a button, or clickenter
to move forward.– Chun
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.
– Clayderson Ferreira
That solution served you @Andersonsantos ?
– Chun