<script type="text/javascript">
var timeout;
function doAjaxFunc(){
alert("called");
$.ajax({
type: "POST",
url: "searchSuggest.php",
data: dataString,
cache: false,
success: function(data){$("#display").html(data).show();}});
}
$(document).ready(function(){
$(".search").keyup(function()
{
var searchbox = $(this).val();
var dataString = 'searchword='+ searchbox;
if(searchbox=='')
{
$("#display").hide();
}
else
{
if(timeout) {
clearTimeout(timeout);
timeout = null;
}
timeout= setTimeout(doAjaxFunc(), 5000);
}
return false;
});
});
</script>
Using this, what I think is the javascript should call function doAjaxFunc() after five second of key is typed. but it is not waiting that period of time. What should I do to make it wait 5 seconds before doAjaxFunc() is executed.
You are CALLING
doAjaxFuncand setting its return value to be the function to be called after five seconds.Remove the
():timeout= setTimeout(doAjaxFunc, 5000)and it will all magically work.