Can you delay this Ajax Search?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can you delay this Ajax Search?

Post by simonmlewis »

Code: Select all


function precheck(str)
{
if (str=="")
  {
  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("srcHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","/presearch.php?q="+str,true);
xmlhttp.send();
}
We have issues on one of our web sites that use this script, as it checks the database on every "keyup". And images it loads are from another source.

Can this be set to run after a delay of so many milliseconds? So you start typing but when you stop, after say, 2 seconds, it then runs?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can you delay this Ajax Search?

Post by Celauran »

I'd start by checking that the user has entered more than X characters (x could be 2, 3, 4.. see what works best for you). Then you want to wrap your trigger in a timeout.

Code: Select all

var delay = (function() {
    var timer = 0;
    return function(callback, ms) {
        clearTimeout (timer);
        timer = setTimeout(callback, ms);
    };
})();
$('#some_element').on('keyup', function(e) {
    delay(function() {
        // AJAX callback goes here
    }, 100);
});
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you delay this Ajax Search?

Post by simonmlewis »

Yes - I think all the above would be good.
What is #some_element'?

I'm assuming it's important as so far it's not doing anything.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Can you delay this Ajax Search?

Post by Weirdan »

Actually it's possible to augment the initial function in run time here:

Code: Select all

precheck = delay(precheck, 100);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you delay this Ajax Search?

Post by simonmlewis »

Sorry how - where??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you delay this Ajax Search?

Post by simonmlewis »

I've been reading this, but I don't understand how to "plug it in" to my code.

http://stackoverflow.com/questions/1909 ... eyup-delay
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can you delay this Ajax Search?

Post by Celauran »

I've already given you a jQuery example and Weirdan showed you how you could accomplish the same with your existing code. Where are you stuck?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can you delay this Ajax Search?

Post by simonmlewis »

I don't know where that code of Weirdan goes - I'm not a jQuery expert at all. I can spot things and make adjustments, but plugging in bits to existing jQuery stuff is out of my skillset.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply