<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t" -->

---
title: Press and Hold (Long Press) Button in JavaScript | daily.dev
description: You can build a press and hold button with pointer events and a requestAnimationFrame timer that fires a custom pressHold event after 800 milliseconds. The...
canonical: https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Press and Hold (Long Press) Button in JavaScript | daily.dev
og:description: You can build a press and hold button with pointer events and a requestAnimationFrame timer that fires a custom pressHold event after 800 milliseconds. The...
og:url: https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t
og:image: https://api.daily.dev/og/posts/YxU2TSB4t.png
og:image:alt: Press and Hold (Long Press) Button in JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Press and Hold (Long Press) Button in JavaScript

**[Kirupa](https://daily.dev/sources/kirupa)** · 23 min read · 0 upvotes · 0 comments

## Summary

You can build a press and hold button with pointer events and a requestAnimationFrame timer that fires a custom pressHold event after 800 milliseconds. The timer subtracts a saved startTime from performance.now() instead of counting frames. Counting frames would make the hold last half as long on a 120Hz screen as on a 60Hz one. Pointerup, pointerleave, and pointercancel all end the press. Holding Space or Enter works too, with e.repeat ignoring auto-repeated keydown events. touch-action: none and a contextmenu handler stop the browser's own long-press behavior.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/press_and_hold.htm>

## Questions this post answers

### Why does my press and hold timer finish twice as fast on 120Hz screens when I count requestAnimationFrame frames?

Counting frames ties the hold length to the display's refresh rate. Firing at 50 frames takes about 833 milliseconds at 60Hz but only about 417 milliseconds at 120Hz. Save performance.now() when the press starts, then compare the elapsed time on each frame against a duration like 800 milliseconds, so the hold lasts the same everywhere.

_Developers shipping gesture code track browser and input quirks like this on daily.dev._

### Do I need to handle pointercancel for a long press button in JavaScript?

Yes. If you only listen for pointerup, a cancelled press never reports that it ended, and the timer keeps running until the hold completes by itself. The browser fires pointercancel when it takes the pointer away, for example when the device rotates or it decides a touch is a scroll. Call the same reset function as for pointerup and pointerleave.

_Edge cases like cancelled touches are what developers keep learning about on daily.dev._

### How do I stop Android's long-press context menu from interfering with a custom press and hold gesture?

Call preventDefault in a contextmenu event handler on the element, which stops the menu from popping up on a long press. Add touch-action: none so the browser doesn't scroll or zoom when a touch starts there. Also set user-select: none and -webkit-touch-callout: none to prevent text selection and the iOS Safari callout.

_Mobile web quirks like this are worth following on daily.dev as browsers change._

## Similar posts on daily.dev

- [Stop buttons triggering zoom when they’re double tapped](https://daily.dev/posts/stop-buttons-triggering-zoom-when-they-re-double-tapped-xoltks6sd) · Piccalilli · 2 upvotes · 0 comments
- [Make a Circle Follow the Mouse Cursor in JavaScript](https://daily.dev/posts/make-a-circle-follow-the-mouse-cursor-in-javascript-gsdugfcgn) · Kirupa · 0 upvotes · 0 comments
- [Quick Tip: Show Animated Text When Someone Clicks Your Page](https://daily.dev/posts/quick-tip-show-animated-text-when-someone-clicks-your-page-f2ifq8laa) · Medium · 0 upvotes · 0 comments
- [Konami Code on a Keyboard: Detect It in JavaScript](https://daily.dev/posts/konami-code-on-a-keyboard-detect-it-in-javascript-c0weacyez) · Kirupa · 0 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#css](https://daily.dev/tags/css), [#accessibility](https://daily.dev/tags/accessibility)

[View this post on daily.dev](https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Press and Hold (Long Press) Button in JavaScript","url":"https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t"},"datePublished":"2026-11-28T06:30:37.620Z","dateModified":"2026-11-28T06:31:04.232Z","description":"You can build a press and hold button with pointer events and a requestAnimationFrame timer that fires a custom pressHold event after 800 milliseconds. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c4d1bcd9d4fedb25bb31cd749d9f3772?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c4d1bcd9d4fedb25bb31cd749d9f3772?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,css,accessibility","timeRequired":"PT23M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Press and Hold (Long Press) Button in JavaScript"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/press-and-hold-long-press-button-in-javascript-yxu2tsb4t#faq","mainEntity":[{"@type":"Question","name":"Why does my press and hold timer finish twice as fast on 120Hz screens when I count requestAnimationFrame frames?","acceptedAnswer":{"@type":"Answer","text":"Counting frames ties the hold length to the display's refresh rate. Firing at 50 frames takes about 833 milliseconds at 60Hz but only about 417 milliseconds at 120Hz. Save performance.now() when the press starts, then compare the elapsed time on each frame against a duration like 800 milliseconds, so the hold lasts the same everywhere. Developers shipping gesture code track browser and input quirks like this on daily.dev."}},{"@type":"Question","name":"Do I need to handle pointercancel for a long press button in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Yes. If you only listen for pointerup, a cancelled press never reports that it ended, and the timer keeps running until the hold completes by itself. The browser fires pointercancel when it takes the pointer away, for example when the device rotates or it decides a touch is a scroll. Call the same reset function as for pointerup and pointerleave. Edge cases like cancelled touches are what developers keep learning about on daily.dev."}},{"@type":"Question","name":"How do I stop Android's long-press context menu from interfering with a custom press and hold gesture?","acceptedAnswer":{"@type":"Answer","text":"Call preventDefault in a contextmenu event handler on the element, which stops the menu from popping up on a long press. Add touch-action: none so the browser doesn't scroll or zoom when a touch starts there. Also set user-select: none and -webkit-touch-callout: none to prevent text selection and the iOS Safari callout. Mobile web quirks like this are worth following on daily.dev as browsers change."}}]}
```

