Skip to content

Instantly share code, notes, and snippets.

@nihlton
Created October 25, 2020 03:51
Show Gist options
  • Select an option

  • Save nihlton/cfeb311119998c075e1fbc48c4dba1dc to your computer and use it in GitHub Desktop.

Select an option

Save nihlton/cfeb311119998c075e1fbc48c4dba1dc to your computer and use it in GitHub Desktop.

Revisions

  1. nihlton created this gist Oct 25, 2020.
    15 changes: 15 additions & 0 deletions longPressEvents.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    export default function longPressEvents(callback, ms = 500) {
    let timeout = null

    const start = () => (timeout = setTimeout(callback, ms))
    const stop = () => timeout && window.clearTimeout(timeout)

    return callback ? {
    onMouseDown: start,
    onMouseUp: stop,
    onMouseLeave: stop,
    onTouchStart: start,
    onTouchMove: stop,
    onTouchEnd: stop,
    } : {}
    }