0
<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 秒。

4

3 回答 3

3

您正在doAjaxFunc调用并将其返回值设置为五秒后要调用的函数。

删除(): timeout= setTimeout(doAjaxFunc, 5000),这一切都会神奇地起作用。

于 2012-06-22T14:54:46.043 回答
1

试试这个:

timeout = setTimeout(function() {
    doAjaxFunc();
}, 5000);

或这个:

timeout = setTimeout(doAjaxFunc, 5000);
于 2012-06-22T14:56:36.213 回答
0
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 | ");
                            });
                        }

    });
};
于 2012-06-22T16:57:16.513 回答