Detect long-press in javascript

Hi.

I recently had the need to detect a long-press on a button in javascript. I did a search on google on onmousedown, onclick, onmouseup, javascript and found some examples on stackowerflow.com. These examples showed how to set a timeout before executing some function.

So I came up with this solution where I can perform one action depending on how long I press a button.

html:

<!doctype html>
<html>
<head>
 <meta charset="utf-8">
 <title>Timeout</title>
</head>
<body>
 <button id="button">click</button>
 <script type="text/javascript" src="func.js"></script>
</body>

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 > 500 && delta < 1000) {
   alert("more than half second and less than a second:");
 }
 if (delta > 1000) {
   alert("more than a second:");
 }
 });
})(window, document);

I forgot where I found the IIFE syntax example which I build this upon. There are many.

I assume there are many ways to detect a long-press.