JavaScript长按自动

时间:2019-09-20 14:38:24

标签: javascript jquery html

我无法在页面上的项目中添加一滴长的东西

我有一个div,我需要以人类以外的时间在auto之外设置一个长按

我想要的像element.click(),但超时时间长...您能帮我提供示例代码吗?

<div>my div</div>

$('div').mousedown(function(){}

2 个答案:

答案 0 :(得分:0)

如果mousedown事件持续的时间少于例如500毫秒,您是否想中止该操作?在这里:

let timerId;
$('div').mousedown(function(){
    timerId = setTimeout( function() {alert("hi!"); }, 500);
});
$('div').mouseup( function () {
    clearTimeout(timerId);
});

答案 1 :(得分:0)

设置持续时间1秒1000ms

var pressTimer;

$("div").mousedown(function(){ 
   // Set timeout
   pressTimer = window.setTimeout(function() { ... Your Code ...},1000);
}).mouseup(function(){ 
   // clear timeout
   clearTimeout(pressTimer);
});