I am building a search box (input field) which should make a server call to filter a grid with the text being inserted on it but I need to make this in an smart way, I need to fire the server call only if the user has stopped. Right now I’m trying to implement it, but if someone knows how to do it I’ll be very pleased. Anyway, if I do it first I’ll post the answer here… Best Regards, Jaime.
Advertisement
Answer
- When a key is pressed:
- Check if there’s an existing timer – stop it if there is one
- start a timer.
- When the timer expires, call the server method.
var searchTimeout; document.getElementById('searchBox').onkeypress = function () { if (searchTimeout != undefined) clearTimeout(searchTimeout); searchTimeout = setTimeout(callServerScript, 250); }; function callServerScript() { // your code here }