Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6174903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:49:53+00:00 2026-05-23T23:49:53+00:00

I need to have some functionality in my web app where a specific action

  • 0

I need to have some functionality in my web app where a specific action occurs when the user clicks and holds on an element. Think of it like the long press on Android.

I have my div:

<div id="myDiv"
  onmousedown="press()"
  onmouseup="cancel()"
  onmouseout="cancel()"
  onmousemove="cancel()">Long Click Me</div>

and my javascript:

var down = false;

function press()
{
  down = true;
  setTimeout(function() { action(); }, 1500);
}

function cancel()
{
  down = false; // this doesn't happen when user moves off div while holding mouse down!
}

function action()
{
  if (!down)
    return; // if the flag is FALSE then do nothing.

  alert("Success!");
  down = false;
}

This works as long as all I do is press and hold on the element. I have the onmouseout and onmousemove events to call cancel() because I want the user to have the option to change their mind and move the mouse off the element before action() starts.

Unfortunately, it seems that my code does not do this.
In fact, if the use clicks down for a moment, moves the mouse off the div and releases before the 1.5 sec then action() won’t bail out as expected.

Edit: Thanks for your input everyone but it turns out I’m just a little bit special and didn’t see that I forgot a capital letter in my HTML in my onmouseout. The sample code I gave above should work exactly as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:49:53+00:00Added an answer on May 23, 2026 at 11:49 pm

    Of course action() is still called. You didn’t actually cancel the setTimeout() function. I would suspect that maybe in your real code, you have a scoping issue and maybe aren’t testing the same version of the done variable that you think you are.

    A better way than using the down flag would be to keep track of the return value from setTimeout() and actually cancel the timer in the cancel() function. Then, action() will never fire when you don’t want it to. I think it’s also technically a more correct behavior when you mouseout to cancel any chance of the timer firing.

    Also, there is no such thing as:

    bool down = false;
    

    in javascript. It would have to be:

    var down = false;
    

    I would recommend this code:

    var downTimer = null;
    
    function down()
    {
      cancel();
      downTimer = setTimeout(function() { action(); }, 1500);
    }
    
    function cancel()
    {
      if (downTimer) 
      {
        clearTimeout(downTimer);
        downTimer = null;
      }
    }
    
    function action()
    {
      downTimer = null;
      alert("Success!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to evaluate user-defined logical expressions of arbitrary complexity on some
I need to have some information about the scoping in JavaScript. I know that
I have some settings I need in a Javascript file -- servers to connect
I have some .net apps running that I need to monitor for example, then
We have some raw voice audio that we need to distribute over the internet.
We have some COBOL programs in our financial applications which need to interact with
We have some very large data files (5 gig to 1TB) where we need
I have some reports in SQL Server Reporting Services 2005 that I need to
I have some ASP.NET Master Pages located in one assembly. I need to use
I have some workflow to work with in Worflow foundation. I need to follow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.