簡體   English   中英

發送 ajax 請求之前的延遲不起作用

[英]Delay before sending ajax request does not work

當我在<td>上執行 hover 時,它確實會等待 900 毫秒,然后發送大量請求(我總是在這 900 毫秒內超過更多 tds 的 hover)。 我究竟做錯了什么? 為什么只有clearTimeout (評論)有效?

我的觀點是在點擊服務器之前等待,如果用戶在此運行倒計時(900 毫秒)中將鼠標移動到另一個<td> ,則先前的倒計時將中止並發生新的倒計時

        $(function(){
                var isLoading = false;
                $("td").hover(function(){
                        var x = parseInt(0);
                        var position = $(this).attr('id');
                        clearTimeout(timer);
                        var oID = $(this).attr('id');
                        var oData = $(this);
                        var timer = setTimeout(function(){
                                if (position == oID&&!isLoading)
                                {
                                        clearTimeout(timer);
                                        $.ajax({
                                                beforeSend: function(xhr){ var isLoading = true; },
                                                url: 'ajax.php?what=click&position='+position,
                                                success: function(data){
                                                        $('#hovercard').css(oData.offset());
                                                        $('#hovercard').show();
                                                        $('#hovercard').html(data);
                                                }
                                        });
                                }
                        }, 900);
// this only works ->                                             clearTimeout(timer);
                });
        });

您需要將timer存儲在 hover function 之外的 scope 中。 我也不太確定用於isLoading的 scope ,所以如果這不起作用,請嘗試將isLoading移至與timer相同的 scope :

    var timer;
    $(function(){
            var isLoading = false;
            $("td").hover(function(){
                    var x = parseInt(0);
                    var position = $(this).attr('id');
                    clearTimeout(timer);
                    var oID = $(this).attr('id');
                    var oData = $(this);
                    timer = setTimeout(function(){
                        if (position == oID&&!isLoading)
                        {
                                clearTimeout(timer);
                                $.ajax({
                                        beforeSend: function(xhr){ isLoading = true; },
                                        url: 'ajax.php?what=click&position='+position,
                                        success: function(data){
                                                    $('#hovercard').css(oData.offset());
                                                    $('#hovercard').show();
                                                    $('#hovercard').html(data);
                                        }
                                });
                        }
                   }, 900);
           });
   });

你還有其他一些奇怪的東西。 請注意,oID 始終等於 position,因為它們同時設置在同一個 scope 中。 這使得 if 語句中的第一個條件毫無意義。 我還刪除了 beforeSend function 中的var語句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM