Code Monkey home page Code Monkey logo

Comments (2)

fedono avatar fedono commented on May 28, 2024

节流

需要对发起请求非常频繁的事件控制住频率,如在监听window.resize 事件,在固定多久的时间内请求一次。
节流的函数中好像一定要有一个标志位,在执行 setTimeout 中的函数时,会一起把这个标志位设置为初始状态,下次检测为初始状态后,才能再建立一次 setTimeout

节流中不会去取消之前需要执行的函数,只是如果之前的函数还没开始执行的话,那么下一次函数也就不会创建

function throttle(callback, limit) {
  var wait = false;
  return function() {
    if (!wait) {
      wait = true;
      setTimeout(function(){
        callback();
        wait = false;
      }, limit)
    }
  }
}

from fe-questions.

fedono avatar fedono commented on May 28, 2024

参考一下 什么是防抖和节流,以及如何编码实现?

from fe-questions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.