简体   繁体   中英

Hold button to scroll continuously ReactJS

i have two buttons left and right to a div, this div has a scroll bar and all i want to do is , when holding one the the buttons for 1000ms .. the scrollbar moves continously (Like the default implementation in our browsers) i know how to use setTimeout! but the problem is inside the function

Inside the class

holdRightButton = () => {
     // i want to use this element :
     // const slider : HTMLElement = this.state.scrollRef.current;
     this.setState({
        timer : setTimeout(() => {

        }, 1000)
      })
    }
releaseButton = () => {
    clearTimeout(this.state.timer);
}

Inside render():

<Button onMouseDown={this.holdRightButton}
onMouseUp={this.releaseButton}>
          <span className="fas fa-chevron-right" /> 
</Button>

i tried to do for example Element.ScrollBy(1, 0) but you know .. it will execute it only 1 time i also tried a boolean that turns to False when event onMouseUp is sent but the application crashes Any solution ? even if it is totally different

setTimeout do something only one time.

Please use setInterval and clearInterval

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM