开发者

setinterval and load

This is a basic jquery problem I think, but it is very annoying. This is what I am doing:

  1. load a href class="tab" lists file (external) from jquery
  2. click on href and load from external file again
  3. setinterval 1000 refresh the results (2) with load() method AGAIN

Question is: At refresh period, the value fo开发者_C百科r name selector changes every 1 second (because of refresh), that I recently clicked the hrefs from #1.

$('a.tab').live('click',function() {
    var value = $(this).attr('name');
    //....
    setInterval(function(){
        $("div#timed").load('retriever.php', {"update":value} );
    } ,1000);//set interval
    //....
    $(this).undelegate('click');
});


I think that you want to cancel the other interval timers when you start a new one.

edit — oops hold on I'm fixing it ... ok thanks @kingjiv!!

$('a.tab').live('click',function() {
    var value = $(this).attr('name');
    var timers = $('body').data('timers') || {};
    //....
    for (var n in timers) clearInterval(timers[n]);

    timers[value] = setInterval(function(){
        $("div#timed").load('retriever.php', {"update":value} );
    } ,1000);//set interval
    //....
    $(this).undelegate('click');
    $('body').data('timers', timers);
});

As others have said, that "undelegate()" call is probably not doing what you think/want.

edit again — this could be done more intelligently; there really only needs to be one timer id stored on the body, of course. I'm kind-of slow sometimes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜