[英]Tap and hold in javascript. Something doesn't work
I have unknown problem with my small function "tap and hold". 我的小功能“点击并按住”存在未知问题。 Could anyone tell me what is wrong?
谁能告诉我这是怎么回事? Thanks for help.
感谢帮助。
<!DOCTYPE html>
<html>
<body>
<button id="seconds">30</button>
<script type="text/javascript">
var timeoutId = 0;
$('#seconds').on('mousedown', function() {
timeoutId = setTimeout(alert("something"), 1000);
}).on('mouseup mouseleave', function() {
clearTimeout(timeoutId);
});
</script>
</body>
</html>
The problem is that your alert is being invoked immediately. 问题是您的警报将立即被调用。 You need to pass a function to setTimeout, not call a function.
您需要将一个函数传递给setTimeout,而不是调用一个函数。 I've refactored your code to make it a function and now the alert will show after 1 second if you don't move your mouse.
我已经重构了您的代码以使其发挥作用,现在如果您不移动鼠标,警报将在1秒后显示。
var timeoutId = 0; $('#seconds').on('mousedown', function() { timeoutId = setTimeout(function(){alert("something")}, 1000); }).on('mouseup mouseleave', function() { clearTimeout(timeoutId); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="seconds">30</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.