防抖 Debounce

函数防抖就是,延迟一段时间再执行函数,如果这段时间内又触发了该函数,则延迟重新计算;

  1. // 简单实现
  2. function debounce(fn, wait) {
  3. let t
  4. return () => {
  5. let context = this
  6. let args = arguments
  7. if (t) clearTimeout(t)
  8. t= setTimeout(() => {
  9. fn.apply(context, args)
  10. }, wait)
  11. }
  12. }
  1. // underscore.js debounce
  2. //
  3. // Returns a function, that, as long as it continues to be invoked, will not
  4. // be triggered. The function will be called after it stops being called for
  5. // N milliseconds. If `immediate` is passed, trigger the function on the
  6. // leading edge, instead of the trailing.
  7. _.debounce = function(func, wait, immediate) {
  8. var timeout, args, context, timestamp, result;
  9. // 处理时间
  10. var later = function() {
  11. var last = _.now() - timestamp;
  12. if (last < wait && last >= 0) {
  13. timeout = setTimeout(later, wait - last); // 10ms 6ms 4ms
  14. } else {
  15. timeout = null;
  16. if (!immediate) {
  17. result = func.apply(context, args);
  18. if (!timeout) context = args = null;
  19. }
  20. }
  21. };

节流 throttle

节流:函数间隔一段时间后才能再触发,避免某些函数触发频率过高,比如滚动条滚动事件触发的函数。

  1. // 简单实现
  2. function throttle (fn, wait, mustRun) {
  3. let start = new Date()
  4. let timeout
  5. return () => {
  6. // 在返回的函数内部保留上下文和参数
  7. let context = this
  8. let args = arguments
  9. let current = new Date()
  10. clearTimeout(timeout)
  11. let remaining = current - start
  12. // 达到了指定触发时间,触发该函数
  13. if (remaining > mustRun) {
  14. fn.apply(context, args)
  15. start = current
  16. } else {
  17. // 否则wait时间后触发,闭包保留一个timeout实例
  18. timeout = setTimeout(fn, wait);
  19. }
  20. }
  21. }
  1. // underscore.js throttle
  2. //
  3. // Returns a function, that, when invoked, will only be triggered at most once
  4. // during a given window of time. Normally, the throttled function will run
  5. // as much as it can, without ever going more than once per `wait` duration;
  6. // but if you'd like to disable the execution on the leading edge, pass
  7. // `{leading: false}`. To disable execution on the trailing edge, ditto.
  8. _.throttle = function(func, wait, options) {
  9. var context, args, result;
  10. var timeout = null;
  11. var previous = 0;
  12. if (!options) options = {};
  13. var later = function() {
  14. previous = options.leading === false ? 0 : _.now();
  15. timeout = null;
  16. result = func.apply(context, args);
  17. if (!timeout) context = args = null;
  18. };
  19. return function() {
  20. var now = _.now();
  21. if (!previous && options.leading === false) previous = now;
  22. var remaining = wait - (now - previous);
  23. context = this;
  24. args = arguments;
  25. if (remaining <= 0 || remaining > wait) {
  26. if (timeout) {
  27. clearTimeout(timeout);
  28. timeout = null;
  29. }
  30. previous = now;
  31. result = func.apply(context, args);
  32. if (!timeout) context = args = null;
  33. } else if (!timeout && options.trailing !== false) {
  34. timeout = setTimeout(later, remaining);
  35. }
  36. return result;
  37. };
  38. };

js函数防抖、节流实现的更多相关文章

  1. js函数的节流和防抖

    js函数的节流和防抖 用户浏览页面时会不可避免的触发一些高频度触发事件(例如页面 scroll ,屏幕 resize,监听用户输入等),这些事件会频繁触发浏览器的重拍(reflow)和重绘(repai ...

  2. js函数的节流与防抖

    一.防抖&节流 在前端开发中有一部分用户行为会频繁的触发事件执行,而对于DOM的操作.资源加载等耗费性能的处理,很可能会导致界面卡顿,甚至浏览器奔溃.函数的节流与防抖就是为了解决类似需求而产生 ...

  3. JS函数防抖与函数节流

    概念 函数防抖(debounce) 当调用动作过n毫秒后,才会执行该动作,若在这n毫秒内又调用此动作则将重新计算执行时间 函数节流(throttle) 预先设定一个执行周期,当调用动作的时刻大于等于执 ...

  4. js函数防抖和函数节流

    参考链接:https://juejin.im/post/5b651dc15188251aa30c8669 参考链接:https://www.jb51.net/article/158818.htm 在我 ...

  5. 浅谈JS函数防抖及应用场景

    [前言] 在工作中,我们可能碰到这样的问题: 用户在搜索的时候,在不停敲字,如果每敲一个字我们就要调一次接口,接口调用太频繁,给卡住了. 用户在阅读文章的时候,我们需要监听用户滚动到了哪个标题,但是每 ...

  6. 函数防抖节流的理解及在Vue中的应用

    防抖和节流的目的都是为了减少不必要的计算,不浪费资源,只在适合的时候再进行触发计算. 一.函数防抖 定义 在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时:典型的案例就是输入搜索:输入 ...

  7. js ---- 函数防抖

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  8. js 函数的防抖(debounce)与节流(throttle) 带 插件完整解析版 [helpers.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         函数防抖与节流是做什么的?下面进行通俗的讲解. 本文借鉴:h ...

  9. js高阶函数应用—函数防抖和节流

    高阶函数指的是至少满足下列两个条件之一的函数: 1. 函数可以作为参数被传递:2.函数可以作为返回值输出: javaScript中的函数显然具备高级函数的特征,这使得函数运用更灵活,作为学习js必定会 ...

随机推荐

  1. 各种排序算法及其java程序实现

    各种排序算法:冒择路(入)兮(稀)快归堆,桶式排序,基数排序 冒泡排序,选择排序,插入排序,稀尔排序,快速排序,归并排序,堆排序,桶式排序,基数排序 一.冒泡排序(BubbleSort)1. 基本思想 ...

  2. Zabbix监控IO

    导入模板 configuration->templates->import,选择你需要导入的模板文件 #zabbix_agentd配置 # iostat #磁盘读的次数 UserParam ...

  3. NYOJ--水池数目

    //NYOJ--水池数目 #include<iostream> #include<cstring> }; using namespace std; void dfs(int,i ...

  4. android四大组件学习总结以及各个组件示例(2)

    上篇博文讲解了activity.content provider,此篇博文来仔细总结service.broadcast receiver: 3. Service >什么是服务?>windo ...

  5. PXE+kickstart无人值守安装CentOS 6

    本文目录: 1.1 PXE说明 1.2 PXE流程 1.3 部署环境说明 1.4 部署DHCP 1.5 部署TFTP 1.6 提供pxe的bootloader和相关配置文件 1.7 利用原版安装镜像获 ...

  6. Jenkins安装与配置

    Jenkins安装与配置 2 Jenkins安装 在最简单的情况下,Jenkins 只需要两个步骤: 1.下载最新的版本(一个 WAR 文件).Jenkins官方网址: http://Jenkins- ...

  7. 百度之星2017初赛A轮 1001 小C的倍数问题

    小C的倍数问题 Accepts: 1990 Submissions: 4931 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...

  8. securecrt鼠标右键的配置

    在使用的secureCRT的情况下,选择好要复制的内容后点击右键时,会直接在命令行粘贴内容.如果不想右键直接粘贴而是跳出菜单选择,就要进行设置了. 方法如下: options->Gloabal ...

  9. [js高手之路] vue系列教程 - 实现留言板todolist(3)

    通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为todolist. 第一步.使用bootstrap做好布局 <!DOCTYPE ...

  10. 【每天一道算法题】时间复杂度为O(n)的排序

    有1,2,……一直到n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度为O(1),使用交换,而且一次只能交换两个数. 这个是以前看到的算法题,题目不难.但是要求比较多,排序算法中,时间 ...