detect long press in javascript

 <!doctype html>

<html>

<head>

 <meta charset="utf-8">

 <title>Timeout</title>

</head>

<body>

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


</body>



<script>

  (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 > 500 && delta < 1000) {

   alert("more than half second and less than a second:");

 }

 if (delta > 3000) {

   alert("more than 3 second:");

 }

 });

})(window, document);

</script>

Post a Comment

0 Comments