防止用户过快点击的工具类,多按钮同样适用

来源:互联网 发布:淘宝刻公章 不承认 编辑:程序博客网 时间:2024/06/06 01:47
/** * 描述:防止用户连续点击某一按钮 * 创建人:菜籽 * 创建时间:2017/7/28 下午2:15 * 备注: */public class PreventFastClick {    private Map<Integer, Long> resourceIDMap = new HashMap<>();    private static int defaultIntervalTime = 500;    private static PreventFastClick preventRepeatClick;    /**     * 获取实例,用户可指定间隔时间     *     * @param intervalTime     * @return     */    public synchronized static PreventFastClick getInstance(int intervalTime) {        defaultIntervalTime = intervalTime;        if (preventRepeatClick == null) {            preventRepeatClick = new PreventFastClick();        }        return preventRepeatClick;    }    /**     * 获取实例,使用默认间隔时间     *     * @return     */    public synchronized static PreventFastClick getInstance() {        if (preventRepeatClick == null) {            preventRepeatClick = new PreventFastClick();        }        return preventRepeatClick;    }    /**     * 告诉用户是否是过快点击     *     * @param resourceID     * @return     */    public boolean isFastClick(int resourceID) {        if (!resourceIDMap.containsKey(resourceID)) {            resourceIDMap.put(resourceID, System.currentTimeMillis());            return false;        }        long firstTime = resourceIDMap.get(resourceID);        resourceIDMap.put(resourceID, System.currentTimeMillis());        if (System.currentTimeMillis() - firstTime < defaultIntervalTime) {            return true;        }        return false;    }}
阅读全文
0 0
原创粉丝点击