• 执行控制——节流模式


    节流模式:对重复的业务逻辑进行控制,执行最后一次操作,并取消其他操作,以提高性能。

    重复的业务逻辑真的很让人讨厌的,但其中往往蕴含着可被优化的空间。

    比如我们经常碰到的一种情况:当鼠标移进容器的时候,改变容器的颜色;当鼠标移出去的时候,恢复默认颜色。

    但是有时候是用户不小心移进来的,或者是不小心移出去的,但是效果却消失了。这样用户的体验效果是非常不好的,这时候,我们就可以利用节流模式。

    节流模式的核心思想是创造计时器,延迟程序的执行。这也使得计时器中的回调函数的操作异步执行,

    源代码:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>节流器试用——huansky</title>
    </head>
    <body>
        <div id="ttt">
            <p id="t1">我只有在鼠标放置两秒后才改变背景色,移出超过两秒才恢复</p>
                <br>
            <p id="t2">我是正常的,只要鼠标移进来就改变背景色,移出就恢复</p>
            <br>
            <p id="t3">我只有在鼠标放置两秒后才改变背景色,移出超过两秒才恢复</p>
        </div>
    </body>
    <script>
        /** 节流器
          *    @param  isclear  获取传入的第一个参数
          * @param  fn       第二个参数,表示函数
          **/
        var throttle=function  () {
    
            var isclear=arguments[0],fn;
            if (typeof isclear==="boolean"){
                fn=arguments[1];
    
                //函数的计时句柄存在,就清除函数
                fn._throttleID && clearTimeout(fn._throttleID);
                if(fn._throttleI){
                    console.log(fn._throttleID);
                }
            }
            else{
                fn=isclear;
                param=arguments[1];
    
                var p={
                    context:null,
                    args:[],
                    time:800,
                };
    
                //清除执行句柄函数
                arguments.callee(true,fn);
                //为函数绑定计时器的句柄,延迟执行函数
                fn._throttleID=setTimeout(function(){
                    //console.log(fn._throttleID);
                    fn.apply(p.context,p.args);
                },p.time)
            }
        }
    
        var dom=document.getElementsByTagName("p");
        var Entefn1=function(){ dom[0].style.cssText="background-color:yellow";};
        var Entefn2=function(){ dom[1].style.cssText="background-color:blue";};
        var Entefn3=function(){ dom[2].style.cssText="background-color:red";};
        var Entefn11=function(){ dom[0].style.cssText="background-color:#fff";};
        var Entefn22=function(){ dom[1].style.cssText="background-color:#fff";};
        var Entefn33=function(){ dom[2].style.cssText="background-color:#fff";};
    
        dom[0].addEventListener("mouseover",function(){
            throttle(true,Entefn11);
            throttle(Entefn1);
        },false);
    
        dom[1].addEventListener("mouseover",Entefn2,false);
    
        dom[2].addEventListener("mouseover",function(){
            throttle(true,Entefn33);
            throttle(Entefn3);
        },false);
    
        dom[0].addEventListener("mouseout",function(){
            throttle(Entefn11);
            throttle(true,Entefn1);
        },false);
    
        dom[1].addEventListener("mouseout",Entefn22,false);
    
        dom[2].addEventListener("mouseout",function(){
            throttle(Entefn33);
            throttle(true,Entefn3);
        },false);    
    </script>
    </html>
    View Code
  • 相关阅读:
    php算法之选择排序
    php算法之插入排序
    五分钟读懂UML类图
    ZC_知识点
    JNI_C#
    JNI_Z
    20160419__第1课_第6课
    Hibernate异常_01
    ZC_注意点
    ZC_知识点
  • 原文地址:https://www.cnblogs.com/huansky/p/5450246.html
Copyright © 2020-2023  润新知