1

I have trouble adding a long drop to an item on the page

I have a div and I need to set a long click with time outside auto as a human

I want is like element.click() but with a long timeout... Can you help me with example code?

<div>my div</div>

$('div').mousedown(function(){}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • What is a 'long click'? what do you mean by it? – MKougiouris Sep 20 '19 at 14:39
  • start a timer on mouse down, and stop it on mouseup, and see if that time meets the minimum for your long lick – johnny 5 Sep 20 '19 at 14:41
  • I want is like. element.click (). But with a long timeout .. can you help me with example code – Mohammed Ibrahim Sep 20 '19 at 14:46
  • function long(){ setTimeout(function(){ alert('long click')},5000);} you can use If you want function to be triggered after some delay, i think you are not clarifying the long click meaning If it doesn't help you. – Faseeh Haris Sep 20 '19 at 14:59
  • 1
    I tried to make the English a little more coherent to a native speaker, but I'm really not sure what "with time outside auto as a human" means. – Heretic Monkey Sep 20 '19 at 15:37
  • Possible duplicate of [Long Press in JavaScript?](https://stackoverflow.com/questions/2625210/long-press-in-javascript) – Heretic Monkey Sep 20 '19 at 15:38

2 Answers2

0

Suppose you want to abort the operation if mousedown event lasts less then, for example, 500 milliseconds? Here it is:

let timerId;
$('div').mousedown(function(){
    timerId = setTimeout( function() {alert("hi!"); }, 500);
});
$('div').mouseup( function () {
    clearTimeout(timerId);
});
PolYarBear
  • 133
  • 2
  • 9
0

set duration for 1 second 1000ms

var pressTimer;

$("div").mousedown(function(){ 
   // Set timeout
   pressTimer = window.setTimeout(function() { ... Your Code ...},1000);
}).mouseup(function(){ 
   // clear timeout
   clearTimeout(pressTimer);
});
  • @Meaningsinminds It's an event listener, if you're not able to work out what this code is doing I suggest you have a read through the jQuery documentation https://api.jquery.com/category/events/ – DBS Sep 20 '19 at 16:14