Reputation: 5712
<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.
Upvotes: 0
Views: 1487
Reputation: 21
try this......
$(".search").keyup(function () {
var searchbox = $(this).val();
var dataString = 'searchword=' + searchbox;
if (searchbox == '') {
$("#display").hide();
}
else {
doAjaxFunc();
}
return false;
});
function doAjaxFunc(){
alert("called");
$.ajax({
type: "POST",
url: "searchSuggest.php",
data: dataString,
cache: false,
success: function (data) {
$("#display").html(data.d).show();
} ,
beforeSend: function () {
$("img").fadeIn(1200, function ()
{
$("div").append(" | beforeSend finished | ");
});
}
});
};
Upvotes: 0
Reputation: 14219
Try this:
timeout = setTimeout(function() {
doAjaxFunc();
}, 5000);
or this:
timeout = setTimeout(doAjaxFunc, 5000);
Upvotes: 1
Reputation: 324610
You are CALLING doAjaxFunc
and 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.
Upvotes: 3