可以用作双击检查,也可以用做时间间隔检查工具。
import java.util.HashMap;
import java.util.Map;
/**
* 防止重复点击
* @class describe
* @anthor alias.su
* @time 2022/11/22 12:12
* @change * @chang time * @class describe
*/
public class ClickCheck {
private static Map<Integer, Long> hashCods = new HashMap<>();
public static boolean doubleCLick(Object o) {
int code = o.hashCode();
long currentTimeMillis = System.currentTimeMillis();
if (hashCods.containsKey(code)) {
Long lastClickTime = hashCods.get(code);
if (currentTimeMillis - lastClickTime > 1000) {
hashCods.put(code, currentTimeMillis);
return false;
} else {
return true;
}
} else {
hashCods.put(code, currentTimeMillis);
}
return false;
}
/**
*
* @param o
* @param duration 毫秒
* @return
*/
public static boolean doubleCLick(Object o, long duration) {
int code = o.hashCode();
long currentTimeMillis = System.currentTimeMillis();
if (hashCods.containsKey(code)) {
Long lastClickTime = hashCods.get(code);
if (currentTimeMillis - lastClickTime > duration) {
hashCods.put(code, currentTimeMillis);
return false;
} else {
return true;
}
} else {
hashCods.put(code, currentTimeMillis);
}
return false;
}
}