关于js中对于函数节流的理解与小案例

          以下是本人对函数节流的粗略理解。

          在js中,我们可能会遇到一些好恶心的情况需要用到一些高频触发的事件。例如,用鼠标滚轴滑动界面时要做一些效果,这时需要监听滚动事件。又或者遇到一些比皮皮虾还皮的孩子或者网购控一直对这个按钮猛点击。为了防止这类骚操作的诞生,我们就需要用到函数节流来处理了。

        

          说到函数节流,典型的就是定时器了。话不多说,先上码!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <style type="text/css">
        
        .chart{
            width: 150px;
            height: 50px;
            border: 2px solid #000;
            text-align: center;
            line-height: 50px;
            float: right;
            margin-right: 100px;
            margin-top: 50px;
        }

        .chart em{
            font-style: normal;
            color: red;
            font-weight: bold;
        }

        .add{
            width: 100px;
            height: 50px;
            background-color: green;
            border: 0;
            color: #fff;
            float: left;
            margin-top: 300px;
            margin-left: 300px;
        }

        .point{
            width: 16px;
            height: 16px;
            background-color: red;
            position: fixed;
            left: 0;
            top: 0;
            display: none;
            z-index: 9999;
            border-radius: 50%;
        }
    </style>

    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        
        $(function(){

            var $chart = $('.chart');
            var $count = $('.chart em');
            var $btn = $('.add');
            var $point = $('.point');

            var $w01 = $btn.outerWidth();
            var $h01 = $btn.outerHeight();

            var $w02 = $chart.outerWidth();
            var $h02 = $chart.outerHeight();


            $btn.click(function(){

                var oPos01 = $btn.offset();
                var oPos02 = $chart.offset();

                $point.css({'left':oPos01.left+parseInt($w01/2)-8,'top':oPos01.top+parseInt($h01/2)-8});
                $point.show();

                $point.animate({'left':oPos02.left+parseInt($w02/2)-8,'top':oPos02.top+parseInt($h02/2)-8},800,function(){
                    $point.hide();
                    var iNum = $count.html();
                    $count.html(parseInt(iNum)+1);
                });

            });

        });
    </script>


</head>
<body>
    <div class="chart">购物车<em>0</em></div>
    <input type="button" name="" value="加入购物车" class="add">
    <div class="point"></div>
</body>
</html>

效果图如下:

上面是没有加上定时器的,结果在多次快速点击的时候,动画就会不显示。而且还执行了很多次。

解决方法是通过设置一个定时器来限制点击次数,代码如下:

PS: 啥都没改,就加了个定时器哈 

.....

$btn.click(function(){

                // 通过设置一个定时器来限制点击次数,从而修复多次快速点击动画不显示的bug
                // 这也是实现函数节流的一个典型例子。关于函数节流后面会马上开讲。
                clearTimeout(timer);
                $point.hide();
                timer = setTimeout(function(){

                    var oPos01 = $btn.offset();
                    var oPos02 = $chart.offset();

                    $point.css({'left':oPos01.left+parseInt($w01/2)-8,'top':oPos01.top+parseInt($h01/2)-8});
                    $point.show();

                    $point.animate({'left':oPos02.left+parseInt($w02/2)-8,'top':oPos02.top+parseInt($h02/2)-8},800,function(){
                        $point.hide();
                        var iNum = $count.html();
                        $count.html(parseInt(iNum)+1);
                    });
                },200);

            });

.....

 

好了,以上就是我的小结~大牛不要打我,我怕疼

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值