Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

102
Views
Need to disable long click on image element

I have the following code on this StackBlitz:

https://stackblitz.com/edit/react-vmstl9

import React from 'react';
import './style.css';

export default function App() {
  return (
    <div>
      <h1>Try on Google Chrome Desktop</h1>
      <p>Open the console log to see how the event gets triggered.</p>
      <p>The event should not get triggered if there is a long click.</p>
      <img
        src="https://via.placeholder.com/200.png/09f/fff"
        onClick={() => {
          console.log('You clicked me!');
        }}
      />
    </div>
  );
}

I need the click event to get triggered only if a normal click happens.

Currently if I click the image and holds the click for 2 seconds, the event still triggers. I need to prevent that.

Any idea on how to achieve that?

Thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I found this article on Stack Overflow found here

const myComponent = () => {

    let clickHoldTimer = null;

    const handleMouseDown = () => {
        clickHoldTimer = setTimeout(() => {
            //Action to be performed after holding down mouse
        }, 1000); //Change 1000 to number of milliseconds required for mouse hold
    }

    const handleMouseUp = () => {
        clearTimeout(clickHoldTimer);
    }

    return (
        <div onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} />
    )

}
about 4 years ago · Juan Pablo Isaza Report

0

you can do this hacks by get holder Time

https://stackblitz.com/edit/react-d1txdm

export default function App() {
let holdTimer;
return (
  <div>
    <h1>Try on Google Chrome Desktop</h1>
    <p>Open the console log to see how the event gets triggered.</p>
    <p>The event should not get triggered if there is a long click.</p>
    <img
      src="https://via.placeholder.com/200.png/09f/fff"
      onClick={(e) => {
        if (holdTimer > 1000) return;
        else console.log('normal click');
      }}
      onMouseDown={() => {
        holdTimer = new Date().getTime();
      }}
      onMouseUp={() => {
        let endHolder = new Date().getTime();
        holdTimer = endHolder - holdTimer;
       }}
     />
    </div>
  ); 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!