<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>
使用這個,我認爲JavaScript應該在鍵入五秒鐘後調用函數doAjaxFunc()
。但它並沒有等待那段時間。在執行doAjaxFunc()之前,我應該怎麼做才能讓它等待5秒鐘。在執行某個功能之前讓javascript等待幾秒鐘