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的按钮按下侦听器以减少按下之间的超时的主要内容,如果未能解决你的问题,请参考以下文章