Detect how long an HTML button is pressedjQuery plugin to detect Konami cheat code sequence“Enter key pressed” event-handlerBetter way to check if Javascript Date object existsIs there a way to avoid calling the document with jQuery on click?Enabling/Disabling button on a certain conditionRefactor three state buttonDetecting two keys pressed in quick successionReplace HTML input file element with custom styled buttonNote which colour button was clickedActivate submit button on form change and detach event listener

Animating wave motion in water

pipe commands inside find -exec?

How to read string as hex number in bash?

What is the reasoning behind standardization (dividing by standard deviation)?

UK Tourist Visa- Enquiry

Have any astronauts/cosmonauts died in space?

Writing in a Christian voice

Help with identifying unique aircraft over NE Pennsylvania

Why do I have a large white artefact on the rendered image?

Triple Trouble Tribond

What is the tangent at a sharp point on a curve?

Emojional cryptic crossword

The English Debate

Does convergence of polynomials imply that of its coefficients?

Don't understand why (5 | -2) > 0 is False where (5 or -2) > 0 is True

Why is "la Gestapo" feminine?

CLI: Get information Ubuntu releases

Does fire aspect on a sword, destroy mob drops?

Norwegian Refugee travel document

Exposing a company lying about themselves in a tightly knit industry: Is my career at risk on the long run?

Weird lines in Microsoft Word

When did hardware antialiasing start being available?

How to balance a monster modification (zombie)?

Homology of the fiber



Detect how long an HTML button is pressed


jQuery plugin to detect Konami cheat code sequence“Enter key pressed” event-handlerBetter way to check if Javascript Date object existsIs there a way to avoid calling the document with jQuery on click?Enabling/Disabling button on a certain conditionRefactor three state buttonDetecting two keys pressed in quick successionReplace HTML input file element with custom styled buttonNote which colour button was clickedActivate submit button on form change and detach event listener













5












$begingroup$


I recently had the need to detect how long a button is pressed and perform different actions based on that. I found some examples on Stack Overflow, but the ones I looked at showed how to use timeout().



I came up with this. There are probably other ways to solve this. While searching, I found an example using IIFE in JavaScript, which I then used for this tiny example.



HTML:



<button id="button">click</button>


JavaScript:



(function(window, document, undefined)
'use strict';
var start;
var end;
var delta;
var button = document.getElementById("button");

button.addEventListener("mousedown", function()
start = new Date();
);

button.addEventListener("mouseup", function()
end = new Date();
delta = end - start;
if (delta > 0 && delta < 500)
alert("less than half second:");

if (delta > 1000)
alert("more than a second:");

);
)(window, document);









share|improve this question











$endgroup$











  • $begingroup$
    Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers.
    $endgroup$
    – Heslacher
    Dec 11 '15 at 10:55






  • 1




    $begingroup$
    Click the button, hold mouse down, move mouse off button, release button. No alert. Do you want it to alert in that case?
    $endgroup$
    – epascarello
    Dec 11 '15 at 13:57











  • $begingroup$
    No, not in this particular case. I assign it to a + button and give a team a goal. If I hold it for longer than three seconds it shall cancel a goal given to a team.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:22










  • $begingroup$
    I think I see what you mean, I'll test. It shouldn't. I thought about it while making lasagne.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:47















5












$begingroup$


I recently had the need to detect how long a button is pressed and perform different actions based on that. I found some examples on Stack Overflow, but the ones I looked at showed how to use timeout().



I came up with this. There are probably other ways to solve this. While searching, I found an example using IIFE in JavaScript, which I then used for this tiny example.



HTML:



<button id="button">click</button>


JavaScript:



(function(window, document, undefined)
'use strict';
var start;
var end;
var delta;
var button = document.getElementById("button");

button.addEventListener("mousedown", function()
start = new Date();
);

button.addEventListener("mouseup", function()
end = new Date();
delta = end - start;
if (delta > 0 && delta < 500)
alert("less than half second:");

if (delta > 1000)
alert("more than a second:");

);
)(window, document);









share|improve this question











$endgroup$











  • $begingroup$
    Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers.
    $endgroup$
    – Heslacher
    Dec 11 '15 at 10:55






  • 1




    $begingroup$
    Click the button, hold mouse down, move mouse off button, release button. No alert. Do you want it to alert in that case?
    $endgroup$
    – epascarello
    Dec 11 '15 at 13:57











  • $begingroup$
    No, not in this particular case. I assign it to a + button and give a team a goal. If I hold it for longer than three seconds it shall cancel a goal given to a team.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:22










  • $begingroup$
    I think I see what you mean, I'll test. It shouldn't. I thought about it while making lasagne.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:47













5












5








5





$begingroup$


I recently had the need to detect how long a button is pressed and perform different actions based on that. I found some examples on Stack Overflow, but the ones I looked at showed how to use timeout().



I came up with this. There are probably other ways to solve this. While searching, I found an example using IIFE in JavaScript, which I then used for this tiny example.



HTML:



<button id="button">click</button>


JavaScript:



(function(window, document, undefined)
'use strict';
var start;
var end;
var delta;
var button = document.getElementById("button");

button.addEventListener("mousedown", function()
start = new Date();
);

button.addEventListener("mouseup", function()
end = new Date();
delta = end - start;
if (delta > 0 && delta < 500)
alert("less than half second:");

if (delta > 1000)
alert("more than a second:");

);
)(window, document);









share|improve this question











$endgroup$




I recently had the need to detect how long a button is pressed and perform different actions based on that. I found some examples on Stack Overflow, but the ones I looked at showed how to use timeout().



I came up with this. There are probably other ways to solve this. While searching, I found an example using IIFE in JavaScript, which I then used for this tiny example.



HTML:



<button id="button">click</button>


JavaScript:



(function(window, document, undefined)
'use strict';
var start;
var end;
var delta;
var button = document.getElementById("button");

button.addEventListener("mousedown", function()
start = new Date();
);

button.addEventListener("mouseup", function()
end = new Date();
delta = end - start;
if (delta > 0 && delta < 500)
alert("less than half second:");

if (delta > 1000)
alert("more than a second:");

);
)(window, document);






javascript event-handling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 11 '15 at 17:27









Jamal

30.4k11121227




30.4k11121227










asked Dec 11 '15 at 10:43









kometenkometen

12615




12615











  • $begingroup$
    Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers.
    $endgroup$
    – Heslacher
    Dec 11 '15 at 10:55






  • 1




    $begingroup$
    Click the button, hold mouse down, move mouse off button, release button. No alert. Do you want it to alert in that case?
    $endgroup$
    – epascarello
    Dec 11 '15 at 13:57











  • $begingroup$
    No, not in this particular case. I assign it to a + button and give a team a goal. If I hold it for longer than three seconds it shall cancel a goal given to a team.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:22










  • $begingroup$
    I think I see what you mean, I'll test. It shouldn't. I thought about it while making lasagne.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:47
















  • $begingroup$
    Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers.
    $endgroup$
    – Heslacher
    Dec 11 '15 at 10:55






  • 1




    $begingroup$
    Click the button, hold mouse down, move mouse off button, release button. No alert. Do you want it to alert in that case?
    $endgroup$
    – epascarello
    Dec 11 '15 at 13:57











  • $begingroup$
    No, not in this particular case. I assign it to a + button and give a team a goal. If I hold it for longer than three seconds it shall cancel a goal given to a team.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:22










  • $begingroup$
    I think I see what you mean, I'll test. It shouldn't. I thought about it while making lasagne.
    $endgroup$
    – kometen
    Dec 11 '15 at 15:47















$begingroup$
Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers.
$endgroup$
– Heslacher
Dec 11 '15 at 10:55




$begingroup$
Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers.
$endgroup$
– Heslacher
Dec 11 '15 at 10:55




1




1




$begingroup$
Click the button, hold mouse down, move mouse off button, release button. No alert. Do you want it to alert in that case?
$endgroup$
– epascarello
Dec 11 '15 at 13:57





$begingroup$
Click the button, hold mouse down, move mouse off button, release button. No alert. Do you want it to alert in that case?
$endgroup$
– epascarello
Dec 11 '15 at 13:57













$begingroup$
No, not in this particular case. I assign it to a + button and give a team a goal. If I hold it for longer than three seconds it shall cancel a goal given to a team.
$endgroup$
– kometen
Dec 11 '15 at 15:22




$begingroup$
No, not in this particular case. I assign it to a + button and give a team a goal. If I hold it for longer than three seconds it shall cancel a goal given to a team.
$endgroup$
– kometen
Dec 11 '15 at 15:22












$begingroup$
I think I see what you mean, I'll test. It shouldn't. I thought about it while making lasagne.
$endgroup$
– kometen
Dec 11 '15 at 15:47




$begingroup$
I think I see what you mean, I'll test. It shouldn't. I thought about it while making lasagne.
$endgroup$
– kometen
Dec 11 '15 at 15:47










4 Answers
4






active

oldest

votes


















4












$begingroup$

500 and 1000 should be constants.



Where you have them now you need to look through to find them in code when you want to change them. I know this is an example, but they're also meaningless. 500 doesn't tell you why half a second matters, but TIMEOUT tells you what the purpose of the number is. Declare your time values at the top of the function with clear names that denote what they're for.






share|improve this answer









$endgroup$




















    2












    $begingroup$

    The end variable could be removed and you could add a return in the if statements.



    You could use Date.now() instead of creating a new Date object if Internet Explorer < 9 is not a problem. And the "delta > 0" is technically not required.






    share|improve this answer











    $endgroup$












    • $begingroup$
      Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
      $endgroup$
      – kometen
      Dec 11 '15 at 18:50


















    0












    $begingroup$

    window and document are global variables. You don't need to pass them into your function. Why have undefined as a third variable?



    Also, better practice is to only use the word var once. So it would be something like this:



    var start,
    end,
    delta,
    button = document.getElementById('button');





    share|improve this answer









    $endgroup$












    • $begingroup$
      "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
      $endgroup$
      – Joseph
      Dec 11 '15 at 18:20











    • $begingroup$
      That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
      $endgroup$
      – krillgar
      Dec 11 '15 at 19:00










    • $begingroup$
      Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
      $endgroup$
      – kometen
      Dec 11 '15 at 20:20


















    0












    $begingroup$

    just so everyone knows, this solution does not work on my browser, which is Iron Version 65.0.3400.0 (Official Build) (64-bit) (Iron is based on Chrome, so if I had to guess I'd suspect this also doesn't work with the latest Chrome anymore). The problem is that buttons seem to immediately fire an event when clicked. This can be fixed by just changing the html to use: <div id="button">click</button> or you can just give most any other element the id of "button" and it should work. Here's an example of a fiddle I made which uses a font-awesome icon which has been given the id "button": https://jsfiddle.net/f6a1cys9/2/





    share








    New contributor




    rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$












      Your Answer





      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "196"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f113587%2fdetect-how-long-an-html-button-is-pressed%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4












      $begingroup$

      500 and 1000 should be constants.



      Where you have them now you need to look through to find them in code when you want to change them. I know this is an example, but they're also meaningless. 500 doesn't tell you why half a second matters, but TIMEOUT tells you what the purpose of the number is. Declare your time values at the top of the function with clear names that denote what they're for.






      share|improve this answer









      $endgroup$

















        4












        $begingroup$

        500 and 1000 should be constants.



        Where you have them now you need to look through to find them in code when you want to change them. I know this is an example, but they're also meaningless. 500 doesn't tell you why half a second matters, but TIMEOUT tells you what the purpose of the number is. Declare your time values at the top of the function with clear names that denote what they're for.






        share|improve this answer









        $endgroup$















          4












          4








          4





          $begingroup$

          500 and 1000 should be constants.



          Where you have them now you need to look through to find them in code when you want to change them. I know this is an example, but they're also meaningless. 500 doesn't tell you why half a second matters, but TIMEOUT tells you what the purpose of the number is. Declare your time values at the top of the function with clear names that denote what they're for.






          share|improve this answer









          $endgroup$



          500 and 1000 should be constants.



          Where you have them now you need to look through to find them in code when you want to change them. I know this is an example, but they're also meaningless. 500 doesn't tell you why half a second matters, but TIMEOUT tells you what the purpose of the number is. Declare your time values at the top of the function with clear names that denote what they're for.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 11 '15 at 10:47









          SuperBiasedManSuperBiasedMan

          11.8k52660




          11.8k52660























              2












              $begingroup$

              The end variable could be removed and you could add a return in the if statements.



              You could use Date.now() instead of creating a new Date object if Internet Explorer < 9 is not a problem. And the "delta > 0" is technically not required.






              share|improve this answer











              $endgroup$












              • $begingroup$
                Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
                $endgroup$
                – kometen
                Dec 11 '15 at 18:50















              2












              $begingroup$

              The end variable could be removed and you could add a return in the if statements.



              You could use Date.now() instead of creating a new Date object if Internet Explorer < 9 is not a problem. And the "delta > 0" is technically not required.






              share|improve this answer











              $endgroup$












              • $begingroup$
                Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
                $endgroup$
                – kometen
                Dec 11 '15 at 18:50













              2












              2








              2





              $begingroup$

              The end variable could be removed and you could add a return in the if statements.



              You could use Date.now() instead of creating a new Date object if Internet Explorer < 9 is not a problem. And the "delta > 0" is technically not required.






              share|improve this answer











              $endgroup$



              The end variable could be removed and you could add a return in the if statements.



              You could use Date.now() instead of creating a new Date object if Internet Explorer < 9 is not a problem. And the "delta > 0" is technically not required.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 11 '15 at 18:36









              Jamal

              30.4k11121227




              30.4k11121227










              answered Dec 11 '15 at 17:50









              zafzaf

              1235




              1235











              • $begingroup$
                Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
                $endgroup$
                – kometen
                Dec 11 '15 at 18:50
















              • $begingroup$
                Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
                $endgroup$
                – kometen
                Dec 11 '15 at 18:50















              $begingroup$
              Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
              $endgroup$
              – kometen
              Dec 11 '15 at 18:50




              $begingroup$
              Thank you. You are right, delta > 0 ought not be necessary but I included it as a habit. Will try Date.now().
              $endgroup$
              – kometen
              Dec 11 '15 at 18:50











              0












              $begingroup$

              window and document are global variables. You don't need to pass them into your function. Why have undefined as a third variable?



              Also, better practice is to only use the word var once. So it would be something like this:



              var start,
              end,
              delta,
              button = document.getElementById('button');





              share|improve this answer









              $endgroup$












              • $begingroup$
                "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
                $endgroup$
                – Joseph
                Dec 11 '15 at 18:20











              • $begingroup$
                That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
                $endgroup$
                – krillgar
                Dec 11 '15 at 19:00










              • $begingroup$
                Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
                $endgroup$
                – kometen
                Dec 11 '15 at 20:20















              0












              $begingroup$

              window and document are global variables. You don't need to pass them into your function. Why have undefined as a third variable?



              Also, better practice is to only use the word var once. So it would be something like this:



              var start,
              end,
              delta,
              button = document.getElementById('button');





              share|improve this answer









              $endgroup$












              • $begingroup$
                "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
                $endgroup$
                – Joseph
                Dec 11 '15 at 18:20











              • $begingroup$
                That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
                $endgroup$
                – krillgar
                Dec 11 '15 at 19:00










              • $begingroup$
                Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
                $endgroup$
                – kometen
                Dec 11 '15 at 20:20













              0












              0








              0





              $begingroup$

              window and document are global variables. You don't need to pass them into your function. Why have undefined as a third variable?



              Also, better practice is to only use the word var once. So it would be something like this:



              var start,
              end,
              delta,
              button = document.getElementById('button');





              share|improve this answer









              $endgroup$



              window and document are global variables. You don't need to pass them into your function. Why have undefined as a third variable?



              Also, better practice is to only use the word var once. So it would be something like this:



              var start,
              end,
              delta,
              button = document.getElementById('button');






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 11 '15 at 17:56









              krillgarkrillgar

              303212




              303212











              • $begingroup$
                "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
                $endgroup$
                – Joseph
                Dec 11 '15 at 18:20











              • $begingroup$
                That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
                $endgroup$
                – krillgar
                Dec 11 '15 at 19:00










              • $begingroup$
                Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
                $endgroup$
                – kometen
                Dec 11 '15 at 20:20
















              • $begingroup$
                "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
                $endgroup$
                – Joseph
                Dec 11 '15 at 18:20











              • $begingroup$
                That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
                $endgroup$
                – krillgar
                Dec 11 '15 at 19:00










              • $begingroup$
                Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
                $endgroup$
                – kometen
                Dec 11 '15 at 20:20















              $begingroup$
              "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
              $endgroup$
              – Joseph
              Dec 11 '15 at 18:20





              $begingroup$
              "only use the word var once" depends on the developer. Also, it's a readability issue, not knowing what something is until you go else where. You don't know if you're in the middle of an object, an array, variable declaration, an expression etc. until you scroll up to see what came before it.
              $endgroup$
              – Joseph
              Dec 11 '15 at 18:20













              $begingroup$
              That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
              $endgroup$
              – krillgar
              Dec 11 '15 at 19:00




              $begingroup$
              That's what your code gets minimized down to. If you follow the Crockford rules and guidelines for the JavaScript Ninja, you'll have a better idea of where you're at, regardless of how long the list of declarations are. With a single grouping of your declarations at the top of your scope and maintaining that consistency, your code will look a lot cleaner.
              $endgroup$
              – krillgar
              Dec 11 '15 at 19:00












              $begingroup$
              Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
              $endgroup$
              – kometen
              Dec 11 '15 at 20:20




              $begingroup$
              Thank you. I found an example with javascript IIFE when searching on google for anonymous functions and that example also had undefined. But true, unnecessary in this context.
              $endgroup$
              – kometen
              Dec 11 '15 at 20:20











              0












              $begingroup$

              just so everyone knows, this solution does not work on my browser, which is Iron Version 65.0.3400.0 (Official Build) (64-bit) (Iron is based on Chrome, so if I had to guess I'd suspect this also doesn't work with the latest Chrome anymore). The problem is that buttons seem to immediately fire an event when clicked. This can be fixed by just changing the html to use: <div id="button">click</button> or you can just give most any other element the id of "button" and it should work. Here's an example of a fiddle I made which uses a font-awesome icon which has been given the id "button": https://jsfiddle.net/f6a1cys9/2/





              share








              New contributor




              rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$

















                0












                $begingroup$

                just so everyone knows, this solution does not work on my browser, which is Iron Version 65.0.3400.0 (Official Build) (64-bit) (Iron is based on Chrome, so if I had to guess I'd suspect this also doesn't work with the latest Chrome anymore). The problem is that buttons seem to immediately fire an event when clicked. This can be fixed by just changing the html to use: <div id="button">click</button> or you can just give most any other element the id of "button" and it should work. Here's an example of a fiddle I made which uses a font-awesome icon which has been given the id "button": https://jsfiddle.net/f6a1cys9/2/





                share








                New contributor




                rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$















                  0












                  0








                  0





                  $begingroup$

                  just so everyone knows, this solution does not work on my browser, which is Iron Version 65.0.3400.0 (Official Build) (64-bit) (Iron is based on Chrome, so if I had to guess I'd suspect this also doesn't work with the latest Chrome anymore). The problem is that buttons seem to immediately fire an event when clicked. This can be fixed by just changing the html to use: <div id="button">click</button> or you can just give most any other element the id of "button" and it should work. Here's an example of a fiddle I made which uses a font-awesome icon which has been given the id "button": https://jsfiddle.net/f6a1cys9/2/





                  share








                  New contributor




                  rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  $endgroup$



                  just so everyone knows, this solution does not work on my browser, which is Iron Version 65.0.3400.0 (Official Build) (64-bit) (Iron is based on Chrome, so if I had to guess I'd suspect this also doesn't work with the latest Chrome anymore). The problem is that buttons seem to immediately fire an event when clicked. This can be fixed by just changing the html to use: <div id="button">click</button> or you can just give most any other element the id of "button" and it should work. Here's an example of a fiddle I made which uses a font-awesome icon which has been given the id "button": https://jsfiddle.net/f6a1cys9/2/






                  share








                  New contributor




                  rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.








                  share


                  share






                  New contributor




                  rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered 9 mins ago









                  rishirishi

                  1




                  1




                  New contributor




                  rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Code Review Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f113587%2fdetect-how-long-an-html-button-is-pressed%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

                      Memorizing the KeyboardThe Norwegian Foreman''If the B…''The Consonant EaterThe Cherry TreeElle Rend Le Coeur Plus AmoureuxFill in the blanks with the number in wordsState of the UnionFind the missing elementsCircuit DiagramWhat's the name of the game show?

                      名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局