Javascript 在react.js中,我想在完成滚动而不是滚动后调用一个函数

Javascript 在react.js中,我想在完成滚动而不是滚动后调用一个函数,javascript,reactjs,Javascript,Reactjs,在这里,我得到了另一个帖子的建议:- constructor(props) { super(props); } componentDidMount() { window.addEventListener('scroll', this.handleScroll, true); } componentWillUnmount() { window.removeEventListener('scroll', this.handleScroll, true); } handleSc

在这里,我得到了另一个帖子的建议:-

constructor(props) {
  super(props);
}
componentDidMount() {
    window.addEventListener('scroll', this.handleScroll, true);
}

componentWillUnmount() {
    window.removeEventListener('scroll', this.handleScroll, true);
}

handleScroll = () => {
    //any code
}

但是每次滚动时都会调用handleScroll,但我想在滚动完成后调用此函数。

在我的头顶上,无法检测滚动事件的结束

您可以改为使用setTimeout函数,该函数将在每次滚动事件时重置。最后一次发生时,setTimeout将结束并执行

这会给你一些类似的东西:

constructor(props) {
  super(props);
  this.handleScrollTimeoutId = -1;
}

componentDidMount() {
    window.addEventListener('scroll', this.handleScroll, true);
}

componentWillUnmount() {
   window.removeEventListener('scroll', this.handleScroll, true);
}

handleScroll = () => {
    clearTimeout(this.handleScrollTimeoutId);
    this.handleScrollTimeoutId = setTimeout(function () {
        // your code
    }, [your delay]);
}