从一个事件侦听器中触发两个函数(一个被限制,另一个没有)

Firing two functions from an event listener (one throttled, the other not)

我有一个用于滚动事件的事件侦听器,它触发一个负责将元素附加到视口的函数。相同的滚动事件侦听器正在被限制以防止滚动事件的倾盆大雨(更易于管理)。用于节流的库是 throttle-debounce。问题:节流 handlePageScroll 正在节流该方法中包含的所有内容(当然)。

代码:

componentDidMount() {
  window.addEventListener('scroll', throttle(2000, this.handlePageScroll));
}

handlePageScroll = () => {
  someFn() // logic for appending an element, does not need throttling
  this.loadNextBatch(); // logic that actually needs to be throttled
};

我曾尝试从 handlePageScroll 内部进行节流,但没有成功。有没有办法从一个事件监听器中触发两个函数? 提前谢谢你。

这是一个解决方案,通过保持状态:

componentDidMount() {
  let throttled = false
  window.addEventListener('scroll', () => {
    if (!throttled) {
      throttle(2000, () => {
        this.loadNextBatch()
        throttled = false
      })()
      throttled = true
    }
    someFn()
  })
}