• dom——防抖与节流


    高频率触发的业务: 抽奖  登录  动画 网络加载等等需要做防抖或者是节流操作

    通过案例来讲解防抖与节流

    防抖比如:有一个炸弹按一次开始键 就倒计时15分钟触发爆炸,再15分钟以内再次开始按键,从新计时15分钟后触发,这种思想就是防抖是思想

    1. <style>
    2. .box {
    3. width: 100px;
    4. height: 100px;
    5. background-color: brown;
    6. cursor: pointer;
    7. position: absolute;
    8. left: 100px;
    9. }
    10. style>
    11. <div class='box'>666div>
    1. document.onclick = fangdou(function(e) {
    2. console.log(6666)
    3. }, 1000)
    4. function fangdou(cb, delay) {
    5. var timer = null;
    6. return function() {
    7. clearTimeout(timer)
    8. timer = setTimeout(function() {
    9. cb()
    10. }, delay)
    11. }
    12. }

     可以看到我们fangdou()传进去的是一个回调函数

    思想就是在设置的时间内高频的点击,只会触发一次,就一定会涉及到计时器,先给变量一个null值,调用回调函数的时候清空,这样就防止了多次点击调用多次,然后给一个计时器给变量设置中间的间隔时间一直传给计时器

    需要注意的一点是我们的一直调用的是这个函数

    function() {
                        clearTimeout(timer)
                        timer = setTimeout(function() {
                            cb()
                        }, delay)
                    }

    1. document.onmousemove=jieliu(function(){console.log(666)},20)
    2. function jieliu(cb,daley){
    3. var timer=null;
    4. return function(){
    5. if(!timer){
    6. timer=setTimeout(()=>{
    7. cb();
    8. timer=null
    9. },daley)
    10. }
    11. }
    12. }

    节流操作:高频触发的业务  需要让它的频率降下来

    思想是类似的通过计时器来降频,设置一个变量为空,使用if做判断如果变量为空才执行,不为空就没有作用,在计时器之中重新把这个变量设置为空就行了

    他的调用者也是这个函数

    function(){
                        if(!timer){
                          timer=setTimeout(()=>{
                              cb();                          
                              timer=null
                          },daley)                        
                        }
                    }

    以及他们的优化代码

    1. //防抖/节流的代码优化
    2. function jieliu2(cb,daley){
    3. var timer=null;
    4. return function(){
    5. // console.log(66666,timer)
    6. // this是事件函数的this
    7. let arg=arguments
    8. if(!timer){
    9. timer=setTimeout(()=>{
    10. cb.apply(this,arg);//本来是window但是希望
    11. timer=null
    12. },daley)
    13. }
    14. }
    15. }
    16. let shijianhanshu=jieliu2(function(e){
    17. // console.log(e.offsetX,e.pageX)
    18. box.style.left=e.pageX-box.offsetWidth/2+"px"
    19. box.style.top=e.pageY-box.offsetHeight/2+"px"
    20. console.log(11111)
    21. },17)
    22. document.addEventListener("mousemove",shijianhanshu)
    23. document.onclick = fangdou2(function(e) {
    24. console.log(6666,e,this)
    25. }, 1000)
    26. function fangdou2(cb, delay) {
    27. var timer = null;
    28. return function() {
    29. //return这个函数频率很高 想办法让cb()执行慢下来:防抖
    30. let arg=arguments
    31. clearTimeout(timer)
    32. timer = setTimeout(()=>{
    33. cb.apply(this,arg)
    34. }, delay)
    35. }
    36. }

  • 相关阅读:
    OpenAI开发者大会掀起风暴:GPT模型价格狂降50%,应用商店即将亮相,AI技术将引爆全球!
    MySQL 8.0 安装
    18.C++之继承
    SpringBoot2 常用注解
    spring中的DI
    华为性格测试通关指南
    03、Spring中的静态代理&JDK动态代理&CGLB动态代理
    Python之作业(二)
    Leetcode系列(双语)——GO两数之和
    MySQL数据类型
  • 原文地址:https://blog.csdn.net/weixin_53596260/article/details/125902887