小提琴- http://jsbin.com/AYeFEHi/1/edit
有人能解释一下为什么这不管用吗?(在Linux上运行铬)
当按钮关闭2s时,我尝试只执行alertbox,如果不是为了清除超时函数的话。
<!DOCTYPE html>
<html>
<head>
<title>Testing onmousedown setTimeout alert</title>
<meta charset='utf-8'>
<meta name='viewport' content='initial-scale=1.0'>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script type='text/javascript'>
function call() {
alert('Hello World!');
}
$("#alertme").on('mousedown touchstart', function() {
setTimeout(call, 2000);
});
$("#alertme").on('mouseup touchend', function() {
clearTimeout(call, 0);
});
</script>
</head>
<body>
<button id="alertme">Alert me</button>
</body>
</html>发布于 2013-12-23 20:42:43
这不是clearTimeout的工作方式。您需要将setTimeout返回的值传递给它。
function call() {
alert('Hello World!');
}
var callTimeout = null; // hold onto the identifier for the timeout
$("#alertme").on('mousedown touchstart', function() {
callTimeout = setTimeout(call, 2000);
});
$("#alertme").on('mouseup touchend', function() {
clearTimeout(callTimeout); // clear the timeout identifier we saved.
});您可能希望将其包装在jQuery页面中,这样脚本就可以在页面上的任何位置:
$(function() {
var call = function() {
alert('Hello World!');
}
var callTimeout = null;
$("#alertme").on('mousedown touchstart', function() {
callTimeout = setTimeout(call, 2000);
});
$("#alertme").on('mouseup touchend', function() {
clearTimeout(callTimeout);
});
});https://stackoverflow.com/questions/20750857
复制相似问题