Hi there, 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.
- 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 }
I don't know much about JavaScript, but you could use a timer (lets say set to 5 seconds) that gets reset on every change event from your input box. If the user stops typing for more than 5 seconds, the timer expires and triggers the submit.
The problem with that approach is that the submit is triggered on every pause, e.g. if the user stops typing to tale a coffee break. You will have to see if this is acceptable to the users.
You could use calls to setTimeout
that calls your server function (with a maybe 2-3 second delay) on a keypress event.
As soon as akey is pressed, cancel the previous setTimeout
call and create a new one.
Then, 2-3 seconds have elapsed with no keypresses, the server event will be fired.
Hi to everyone, thanks for your feedback. I'd implemented this piece of code and it seems that it does the job (using jquery):
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var interval = 500;
var filterValue = "";
$(document).ready(function() {
$(".txtSearch").bind("keypress", logKeyPress);
});
function logKeyPress() {
var now = new Date().getTime();
var lastTime = this._keyPressedAt || now;
this._keyPressedAt = now;
if (!this._monitoringSearch) {
this._monitoringSearch = true;
var input = this;
window.setTimeout(
function() {
search(input);
}, 0);
}
}
function search(input) {
var now = new Date().getTime();
var lastTime = input._keyPressedAt;
if ((now - lastTime) > interval) {
/*console.log(now);
console.log(lastTime);
console.log(now - lastTime);*/
if (input.value != filterValue) {
filterValue = input.value;
//console.log("search!");
alert("search!");
}
input._monitoringSearch = false;
}
else {
window.setTimeout(
function() {
search(input);
}, 0);
}
}
</script>
Here is a simple way that will work without jQuery:
<input type="text" id="TxtSearch" onchange="countDown=10;" />
<script type="text/javascript">
var countDown = 0;
function SearchTimerTick()
{
if(countDown == 1)
{
StopTypingCommand();
countDown = 0;
}
if(countDown > 0)
countDown--;
}
window.setInterval(SearchTimerTick,1000);
</script>
You also probably want to check the length of the string on the input box, otherwise risk returning a huge result set!