在jQuery中与.bind()和.unbind()搞砸了

时间:2021-09-25 19:42:59

Ok, so i have two ways to perform div refresh

好的,所以我有两种方法来执行div刷新

$('#player').load('/videos/index');

in my rails app. One is through link

在我的rails应用程序中。一个是通过链接

<a href="#" id="nextb" onclick="$('#player').load('/videos/index').unbind();">next</a>

other is through shortcut of that button. And they work fine, until i discovered, that if I refresh player twice in a row through the link, and then through the shortcut, it loads div twicely. If i refresh through the link three times in a row, than through the shortcut, than it's loading div three times. And so on.. So, i've started to trying configure binds (as I assumed, that was the reason), but there were mo result. Here is my messed up in binds shortcut part of application.js

另一种是通过该按钮的快捷方式。他们工作正常,直到我发现,如果我通过链接连续两次刷新播放器,然后通过快捷方式,它会两次加载div。如果我连续三次通过链接刷新,而不是通过快捷方式,而不是加载div三次。等等..所以,我已经开始尝试配置绑定(正如我所假设的,这就是原因),但有mo结果。这是我在application.js的绑定快捷方式部分搞砸了

 $(document).ready(function(){;
$(document).bind('keydown', 'space', function() {
    $('#player').unbind('load');    
    $('#player').load('/videos/index');
    $(document).unbind();
    }); 
 });

I tried to click on link through the shortcut, but the same result, so I've assume, the problem is in load. Any suggestions would help.

我尝试通过快捷方式单击链接,但结果相同,所以我假设,问题是加载。任何建议都会有帮助。

1 个解决方案

#1


1  

The best way to avoid a double refresh, is having an state flag, indicating if it can load again, or if it's loading:

避免双重刷新的最佳方法是使用状态标志,指示它是否可以再次加载,或者是否正在加载:

$(document).ready(function() {
    var loading = false;
    function reloadDiv() {
        if(!loading) {
            loading = true;
            $('#player').load('/videos/index', null, function() { loading = false; });
        }
    }  
    $('#nextb').click(reloadDiv);
    $(document).bind('keydown', 'space', reloadDiv);
});

Good luck!

祝你好运!

#1


1  

The best way to avoid a double refresh, is having an state flag, indicating if it can load again, or if it's loading:

避免双重刷新的最佳方法是使用状态标志,指示它是否可以再次加载,或者是否正在加载:

$(document).ready(function() {
    var loading = false;
    function reloadDiv() {
        if(!loading) {
            loading = true;
            $('#player').load('/videos/index', null, function() { loading = false; });
        }
    }  
    $('#nextb').click(reloadDiv);
    $(document).bind('keydown', 'space', reloadDiv);
});

Good luck!

祝你好运!