I have a search box which uses AJAX to query a database and return a list of suggestions to the user, it works off the “onkeyup” event on the input textbox.
Obviously this creates an AJAX call for every keypress the user makes and I’d like to make this more efficient by adding a short delay before the AJAX call is made.
How would I modify my code to make this happen?
JavaScript code below:
<script type="text/javascript">
function showHint(str)
{
var xmlhttp;
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.asp?q="+str,true);
xmlhttp.send();
}
</script>
Another thing you could add is to only start a search after two or three characters have been entered. That, combined with Logic Ali’s suggestion above should significantly reduce the number of requests.
The misplaced bracket? Yeah I saw that, but even with the correction I get an error which throws back to the original script… it looks like some kind of incompatibility between your script and my implementation of Immerse’s suggestion to wait until at least 3 characters were typed.
Message: ‘length’ is null or not an object
Line: 14
Char: 1
Code: 0
I don’t get this error without your delay code in place but its referencing the following line in the main script which makes the Ajax call.:
if (str.length<3)
Should add, I appreciate your help with this, Javascript has never been my strong point.
That seems to implement the delay but messes up the Ajax call, I get no matches on my Ajax query where before I had them. It also seems to cause the Ajax call to happen instantly after 1 second in Internet Explorer, even if no keypress is made.
That fixes the IE instant execution, but I still have the problem with my Ajax call, since I put in the “String(str)” change you suggested, it just doesn’t seem to make the call at all.
Its as if the following condition is always true even though I have typed 3+ letters into the search box.
if (String(str).length<3)
{
document.getElementById("txtHint").innerHTML="";
return;
}
Or I suppose it could be that the timer is not clearing, (those are milliseconds right?) either way I’m just getting nothing from my ajax call even though previously I was.
Sorry to bounce this thread, but I have another query.
I know we spent a lot of time on this example reducing the number of AJAX calls so there was no more than 1 per second and only after at least 3 characters had been typed into the search box.
But I now want to hotlink to the page that makes this AJAX call with a pre-defined search parameter in the QueryString, so I need to the script to make the Ajax call as the page first loads (but still only if the search parameter contains at least 3 characters) as well as when there is a change to the search textbox.