节流函数 只执行第一次 和 只执行在以后一次

节流函数 只执行第一次


      let timer = null
      function throttle(fn, wait) {
        return function() {
            if (!timer) {
              // 函数fn放在定时器外且定时器初始化放在throttle函数外 是只执行第一次;
              // 函数fn放在定时器内且定时器初始化放在throttle函数内 是只执行最后一次
              fn.apply(this, Array.prototype.slice.call(arguments, 0));
              timer = setTimeout(() => {
                timer = null;
              }, wait);
            }
        }
      }

节流函数 只执行最后一次

      function throttle(fn, wait) {
      let timer = null
        return function() {
            if (!timer) {
              // 函数fn放在定时器外且定时器初始化放在throttle函数外 是只执行第一次;
              // 函数fn放在定时器内且定时器初始化放在throttle函数内 是只执行最后一次
              timer = setTimeout(() => {
                fn.apply(this, Array.prototype.slice.call(arguments, 0));
                timer = null;
              }, wait);
            }
        }
      }
监听鼠标滚轮事件 切换页面的轮播 (只执行第一次)
$(function () {
      let index = 0;
      let time = null
      var timer = null
      function startUp () {
        time = setInterval(() => {
          if (index < 4) {
            index += 1
          } else {
            index = 0
          }
          swiper()
        }, 6000);
      }
      swiper();
      startUp();
      function swiper() {
        $('.swiper-slide').removeClass('active')
        $('.swiper-slide').eq(index).addClass('active')
        $('.pager-item').removeClass('active')
        $('.pager-item').eq(index).addClass('active')
      }
      // 节流函数
      function throttle(fn, wait) {
        return function() {
            if (!timer) {
              // 函数fn放在定时器外且定时器初始化放在throttle函数外 是只执行第一次;
              // 函数fn放在定时器内且定时器初始化放在throttle函数内 是只执行最后一次
              fn.apply(this, Array.prototype.slice.call(arguments, 0));
              timer = setTimeout(() => {
                timer = null;
              }, wait);
            }
        }
      }

      window.addEventListener('mousewheel', throttle((e) => {
        e = e || window.event;
        clearInterval(time);
        if (e.wheelDelta) {  //判断浏览器IE,谷歌滑轮事件
          if (e.wheelDelta > 0) { //当滑轮向上滚动时
            if (index === 0) {
              index = 4
            } else {
              index -= 1
            }
          }
          if (e.wheelDelta < 0) { //当滑轮向下滚动时
            if (index < 4) {
              index += 1
            } else {
              index = 0
            }
          }
        } else if (e.detail) {  //Firefox滑轮事件
          if (e.detail> 0) { //当滑轮向上滚动时
            if (index === 0) {
              index = 4
            } else {
              index -= 1
            }
          }
          if (e.detail< 0) { //当滑轮向下滚动时
            if (index < 4) {
              index += 1
            } else {
              index = 0
            }
          }
        }
        swiper();
        startUp();
      }, 2000))
    })
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本节介绍各种常见的浏览器事件。 鼠标事件 鼠标事件指与鼠标相关的事件,主要有以下一些。 click 事件,dblc...
    许先生__阅读 2,853评论 0 4
  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,715评论 1 11
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,683评论 1 32
  • 一直不喜欢无病呻吟,也不喜欢郁郁寡欢,回观近期,居然是我讨厌的状态,生活有什么大不了的,吐槽吐槽着就淡了,过...
    天天都要晒太阳阅读 158评论 1 2
  • 昨天还让人心生怜悯的pptv聚力,今天下午2点07分又再一次发布微博,而几天发布的内容是see U again!再...
    马浩周阅读 315评论 0 0

友情链接更多精彩内容