JSFiddle - React, Tailwind, and code Playground

by dipeshbeckham

HTML

<input type="button" onmousedown="onmousedownFunc()" onmouseup="onmouseupFunc()" value="hold for 2 sec"/>

JavaScript

var timer;
var istrue = false;
var delay = 2000;

function onmousedownFunc()
{
   istrue = true;
   timer = setTimeout(function(){ makeChange();},delay);
}

function makeChange()
{
      if(timer)
      clearTimeout(timer);
      
      if(istrue)
      {
            /// rest of your code
          alert('holding');

      }
}
function onmouseupFunc()
{
   istrue =false;
}