繁体   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