如何重置setTimeout

时间:2013-02-10 16:14:58

标签: javascript settimeout

点击后我有这个动作:

  remote_top.on('click',function(){
        if (program_changer_block.css('display','block')){
            var g_timer = null;
            clearTimeout(g_timer);
            setTimeout(function(){
                program_changer_block.fadeOut();
            }, 5100);
        }
        else if (program_changer_block.css('display','none')){
            setTimeout(function(){
                program_changer_block.fadeIn();
            }, 300);
        }
   });

我想要做的是每次点击后清除超时。现在经过一些快速点击后,我的块在第一次点击后计算的5100ms后隐藏。每次点击后如何重启此超时?有什么不对的,你能帮帮我吗?

2 个答案:

答案 0 :(得分:2)

setTimeout()函数返回超时ID 。只需将其存储在g_timer变量中,并将其设为全局变量:

  var g_timer;
  remote_top.on('click',function(){
        if (program_changer_block.css('display','block')){
            clearTimeout(g_timer);
            g_timer = setTimeout(function(){
                program_changer_block.fadeOut();
            }, 5100);
        }
        else if (program_changer_block.css('display','none')){
            g_timer = setTimeout(function(){
                program_changer_block.fadeIn();
            }, 300);
        }
   });

答案 1 :(得分:0)

您是否尝试使用clearQueue直接控制队列并延迟?

remote_top.on('click',function(){
    if(program_changer_block.css('display','block')){
        program_changer_block.stop(true,true).delay(5100).fadeOut();
    }
    else if(program_changer_block.css('display','none')){
        program_changer_block.stop(true,true).delay(300).fadeIn();
    }
});

stop()中的第一个true表示.clearQueue()

相关问题