$('#action').click(function() {
if(setTimeout(function() {}, 1000)) {
alert("Hold");
} else {
alert("Click");
}
})
<!-- begin snippet: js hide: false console: true babel: false -->
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id='action'>Hello</button>
&#13;
如果我点击按钮,我试图提醒Click
,如果我按住按钮2秒,则提示Hold
,
问题是它在点击后始终会提醒Hold
。
如何解决这个问题以及如何计算完全按住按钮的时间?
答案 0 :(得分:1)
试试下面的代码:
$(document).ready(function() {
var timeoutId = 0;
var functionCalled = false;
$("#btn").on("mousedown", function() {
timeoutId = setTimeout(btnHeld, 2000);
}).bind("mouseup", function(e) {
if (!functionCalled) {
alert('clicked');
}
functionCalled = false;
clearTimeout(timeoutId);
});
function btnHeld() {
functionCalled = true;
alert("hold");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btn">click</button>
答案 1 :(得分:0)
您需要处理#! /bin/sh
# Optionally put into temp var.
dt=$(date -I)
Z=$(cat /home/saurabh/scripts/$(date -I))
# or: Z=$(cat /home/saurabh/scripts/$dt)
echo "$Z"
...
事件,将计时器设置为两秒钟,然后在mousedown
事件中取消该计时器。