-1

i want you to have to hold the button down for a second for it to start... I've tried a couple of things like

function startHold1() {
  setTimeout(function(){
    startHold1A = setInterval(func1(), 250) }, 1000)
}

function endHold1() {
  setTimeout(function(){
    clearInterval(startHold1A, 950)
  })
}

func1 basically adds 1 to a variable and displays it on the screen also i have a button that initiates startHold1 onmousedown and endHold1 onmouseup I'm a super noob at code please help!

  • Does this answer your question? [HTML button- when hold the button it will repeat the action](https://stackoverflow.com/questions/53347492/html-button-when-hold-the-button-it-will-repeat-the-action) – rhavelka Feb 26 '21 at 16:58
  • ` setInterval(func1(), 250)` => ` setInterval(func1, 250)` (no `()` after `func`), see the linked question's answers. – T.J. Crowder Feb 26 '21 at 16:59

1 Answers1

-3

var setint  = '';
$(document).ready(function() {
var val = 0;
$('#hold').on('mousedown',function (e) {
   clearInterval(setint);
   val = 0;
   setint = setInterval(function () {
       $("#putdata").val(++val);
        console.log("mousehold");
   },50);
});
$('#hold').on("mouseleave mouseup", function () {
   val = 0;
   $("#putdata").val(val);
   clearInterval(setint);
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<input type="text" id="putdata" />
<input type="button" value="mouse-hold" id="hold" />
</body>
<html>
Akin Okegbile
  • 1,108
  • 19
  • 36
  • adding jquery isn't always the best answer to a javascript question. – rhavelka Feb 26 '21 at 17:02
  • yes.. adding jquery isnt always the best answer.. but it is an answer. and it answers the question at hand... Last time i checked jquery is javascript. feel free to add alternative answers. – Akin Okegbile Feb 26 '21 at 21:36