0

当我将鼠标悬停在它上面时,<td>它会等待 900 毫秒,然后发送大量请求(我总是在这 900 毫秒内将鼠标悬停在更多的 td 上)。我究竟做错了什么?为什么只有clearTimeout (评论)有效?

我的意思是在点击服务器之前等待,如果用户<td>在这个运行倒计时(900 毫秒)中将鼠标移动到另一个,之前的倒计时被中止并发生新的倒计时

        $(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);
                });
        });
4

1 回答 1

1

您将需要存储timer在悬停功能之外的范围内。我不太确定两者的范围isLoading,所以如果这不起作用,请尝试isLoading移出与以下相同的范围timer

    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);
           });
   });

You have some other oddities as well. Note that oID always will be equal to position as they are set at the same time, in the same scope. This makes the first condition in your if statement pointless. I've also removed the var statement in your beforeSend function.

于 2011-07-09T21:08:23.747 回答