javascript 重写Flic的按钮按下侦听器以减少按下之间的超时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 重写Flic的按钮按下侦听器以减少按下之间的超时相关的知识,希望对你有一定的参考价值。

import {
    BUTTON_DOWN,
    BUTTON_UP,
    doublePressAction,
    holdPressAction,
    singlePressAction,
} from './actions'

const timeLimit = 300

let startTime = Date.now()
let timeoutId = 0

const isWithinTimeLimit = () => Date.now() - startTime < timeLimit

const handleButtonPress = buttonState => {
    if (buttonState === BUTTON_DOWN) {
        if (isWithinTimeLimit()) {
            clearTimeout(timeoutId)
            startTime = 0

            doublePressAction()
        }

        else {
            startTime = Date.now()
            timeoutId = (
                setTimeout(holdPressAction, timeLimit)
            )
        }
    }

    else if (buttonState === BUTTON_UP) {
        if (isWithinTimeLimit()) {
            clearTimeout(timeoutId)

            startTime = Date.now()
            timeoutId = (
                setTimeout(singlePressAction, timeLimit)
            )
        }
    }
}

const listenForButtonPress = bluetoothAddress => {
    new FlicConnectionChannel(bluetoothAddress)
    .on('buttonUpOrDown', handleButtonPress)
}

以上是关于javascript 重写Flic的按钮按下侦听器以减少按下之间的超时的主要内容,如果未能解决你的问题,请参考以下文章

javascript 用于按下单按钮和双按钮的Flic按钮。

滚动画廊启用按下状态并从子项中删除点击侦听器

JAVA:如何通过此命令来监听按钮按下?

javascript基础:事件

JavaScript事件

JavaScript----事件