Javascript 使用 setTimeout 单击并按住元素
我需要在我的网络应用程序中提供一些功能,当用户单击并按住某个元素时,会发生特定的操作。可以将其想象为 Android 上的长按。
我有我的 div:
<div id="myDiv"
onmousedown="press()"
onmouseup="cancel()"
onmouseout="cancel()"
onmousemove="cancel()">Long Click Me</div>
和我的 javascript:
var down = false;
function press()
{
down = true;
setTimeout(function() { action(); }, 1500);
}
function cancel()
{
down = false; // this doesn't happen when user moves off div while holding mouse down!
}
function action()
{
if (!down)
return; // if the flag is FALSE then do nothing.
alert("Success!");
down = false;
}
只要我所做的就是按住该元素,它就可以工作。我有 onmouseout
和 onmousemove
事件来调用 cancel(),因为我希望用户可以选择改变主意并在 action() 之前将鼠标从元素上移开开始。
不幸的是,我的代码似乎没有这样做。 事实上,如果用户单击了一会儿,将鼠标从 div 上移开并在 1.5 秒之前释放,那么 action() 将不会按预期退出。
编辑:感谢大家的意见,但事实证明我只是有点特殊并且没有看到我在 onmouseout 中忘记了 HTML 中的大写字母。我上面给出的示例代码应该完全按照预期工作。
I need to have some functionality in my web app where a specific action occurs when the user clicks and holds on an element. Think of it like the long press on Android.
I have my div:
<div id="myDiv"
onmousedown="press()"
onmouseup="cancel()"
onmouseout="cancel()"
onmousemove="cancel()">Long Click Me</div>
and my javascript:
var down = false;
function press()
{
down = true;
setTimeout(function() { action(); }, 1500);
}
function cancel()
{
down = false; // this doesn't happen when user moves off div while holding mouse down!
}
function action()
{
if (!down)
return; // if the flag is FALSE then do nothing.
alert("Success!");
down = false;
}
This works as long as all I do is press and hold on the element. I have the onmouseout
and onmousemove
events to call cancel() because I want the user to have the option to change their mind and move the mouse off the element before action() starts.
Unfortunately, it seems that my code does not do this.
In fact, if the use clicks down for a moment, moves the mouse off the div and releases before the 1.5 sec then action() won't bail out as expected.
Edit: Thanks for your input everyone but it turns out I'm just a little bit special and didn't see that I forgot a capital letter in my HTML in my onmouseout. The sample code I gave above should work exactly as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,action() 仍然被调用。您实际上并没有取消
setTimeout()
函数。我怀疑也许在您的真实代码中,您存在范围问题,并且可能没有测试您认为的相同版本的done
变量。比使用
down
标志更好的方法是跟踪setTimeout()
的返回值,并在cancel()
中实际取消计时器。代码>函数。然后,当您不希望时action()
永远不会触发。我认为,当您将鼠标移出以取消计时器触发的任何机会时,这在技术上也是更正确的行为。不存在这样的东西:
另外, JavaScript 中 它必须是:
我会推荐这段代码:
Of course action() is still called. You didn't actually cancel the
setTimeout()
function. I would suspect that maybe in your real code, you have a scoping issue and maybe aren't testing the same version of thedone
variable that you think you are.A better way than using the
down
flag would be to keep track of the return value fromsetTimeout()
and actually cancel the timer in thecancel()
function. Then,action()
will never fire when you don't want it to. I think it's also technically a more correct behavior when you mouseout to cancel any chance of the timer firing.Also, there is no such thing as:
in javascript. It would have to be:
I would recommend this code:
您还需要清除取消函数中的超时 - 否则它仍然会触发 - 正如您在向下函数中启动它一样。
所以..
You also need to clear the timeout in your cancel function - otherwise it will still fire - as you initiated it in the down function.
so..
据我所知,您的代码有几个问题。
首先
是无效的 JavaScript。应该是
然后你有两个名为
down
的变量:布尔值和函数。该函数将覆盖该变量,直到您执行将down
设置为 true 或 false 的语句之一。正如其他人所说:一旦设置,延迟函数将在1.5秒后继续执行,除非您取消超时。但话又说回来,这并不重要,因为您在执行任何操作之前都会检查鼠标按钮是否按下。
所以我建议将布尔变量重命名为 isMouseDown 或其他名称,然后重试。
There are several things wrong with your code as I see it.
First
is not valid JavaScript. It should be
Then you have two variables called
down
: the boolean value and the function. The function will overwrite the variable, until as such time that you execute one of the statements that setsdown
to true or false.As others have said: once set, the deferred function will continue to be executed 1.5 seconds later, unless you cancel the timeout. But then again it doesn't matter, since you do check to see if the mouse button is down before doing anything.
So I'd say rename the boolean variable to
isMouseDown
or something and try again.要取消 cancel() 函数中的超时,
还请注意,您首先使用
down
作为变量 end,然后作为函数...调用if(!down)
总是返回 false,因为down
引用了一个函数。to cancel the timeout in the cancel() function, use
also note that you used
down
first as a variable end then as a function... Callingif(!down)
will always return false becausedown
refers to a function.