JavaScript定时器如何清除和重启

454次阅读
没有评论

定时器の清除和重启方法:

//
var t=setInterval(fun1,500) //fun1是你の函数
var fun1=function(){
    //写入你の函数;
}
clearInterval(t)//清除定时器
t=setInterval(fun1,500)//重新开始定时器

提示:要先清除,后设置,否则定时器永远清除不了。

例子:

1个可复用の延时显隐下拉菜单のjs(抓元素和show();hide();等方法基于jQuery)

//顶部导航下拉列表
    function showhide(objMouseenter,objshow){
        var timer = null;
        //移入显示
        $(objshow).mouseenter(function(){
            clearTimeout(timer);
        });
        $(objMouseenter).mouseenter(function(){
            clearTimeout(timer);
            $(objshow).show();
        });        
        //移出设置定时器隐藏
        $(objMouseenter).mouseleave(function(){            
            timer = setTimeout(function() {
                $(objshow).hide();
            }, 500);
        }); 
        $(objshow).mouseleave(function(){
            timer = setTimeout(function() {
                $(objshow).hide();
            }, 500);
        });
    }
    showhide('#navFollow','#qrCode');

 

facingscreen
版权声明:本站原创文章,由 facingscreen2022-08-12发表,共计646字。
转载说明:本文为搜栈网原创文章,除特殊说明外皆由CC-4.0协议发布,转载请注明出处,如有帮助欢迎打赏。
评论(没有评论)
验证码