答案
ref + settimeout
疑问
为啥不能使用 state / 静态变量
state 就存在闭包
静态变量无效
实现代码
handleSingleClick = () => {
const timer = setTimeout(() => {
if (!this.isDoubleClick) {
console.log("Single click");
}
this.isDoubleClick = false;
clearTimeout(timer);
this.timerRef.current = null;
}, 200);
this.timerRef.current = timer;
}
handleDoubleClick = () => {
this.isDoubleClick = true;
const timer = this.timerRef.current;
if (timer) {
clearTimeout(timer);
this.timerRef.current = null;
}
console.log("Double click");
}