开发者

Messed up with .bind() and .unbind() in jQuery

Ok, so i have two ways to perform div refresh

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

in my rails app. One is through link

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

other is th开发者_如何学Gorough 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

 $(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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜