[英]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! 我有一个div左右两个按钮,这个div有一个滚动条,我想要做的就是,当按住一个按钮1000ms时......滚动条不断移动(就像我们浏览器中的默认实现一样)我知道如何使用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 ? 我试图做例如Element.ScrollBy(1,0),但是你知道..它只会执行一次我也尝试了一个布尔值,当发送事件onMouseUp但应用程序崩溃任何解决方案时转向False? even if it is totally different
即使它完全不同
setTimeout do something only one time. setTimeout只执行一次。
Please use setInterval and clearInterval 请使用setInterval和clearInterval
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.