Javascript 使用 setTimeout 单击并按住元素

发布于 2024-11-26 15:28:40 字数 984 浏览 13 评论 0原文

我需要在我的网络应用程序中提供一些功能,当用户单击并按住某个元素时,会发生特定的操作。可以将其想象为 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;
}

只要我所做的就是按住该元素,它就可以工作。我有 onmouseoutonmousemove 事件来调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他不在意 2024-12-03 15:28:40

当然,action() 仍然被调用。您实际上并没有取消 setTimeout() 函数。我怀疑也许在您的真实代码中,您存在范围问题,并且可能没有测试您认为的相同版本的 done 变量。

比使用 down 标志更好的方法是跟踪 setTimeout() 的返回值,并在 cancel() 中实际取消计时器。代码>函数。然后,当您不希望时 action() 永远不会触发。我认为,当您将鼠标移出以取消计时器触发的任何机会时,这在技术上也是更正确的行为。

不存在这样的东西:

bool down = false;

另外, JavaScript 中 它必须是:

var down = false;

我会推荐这段代码:

var downTimer = null;

function down()
{
  cancel();
  downTimer = setTimeout(function() { action(); }, 1500);
}

function cancel()
{
  if (downTimer) 
  {
    clearTimeout(downTimer);
    downTimer = null;
  }
}

function action()
{
  downTimer = null;
  alert("Success!");
}

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 the done variable that you think you are.

A better way than using the down flag would be to keep track of the return value from setTimeout() and actually cancel the timer in the cancel() 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:

bool down = false;

in javascript. It would have to be:

var down = false;

I would recommend this code:

var downTimer = null;

function down()
{
  cancel();
  downTimer = setTimeout(function() { action(); }, 1500);
}

function cancel()
{
  if (downTimer) 
  {
    clearTimeout(downTimer);
    downTimer = null;
  }
}

function action()
{
  downTimer = null;
  alert("Success!");
}
画▽骨i 2024-12-03 15:28:40

您还需要清除取消函数中的超时 - 否则它仍然会触发 - 正如您在向下函数中启动它一样。

所以..

bool down = false;
//your timeout var
var t;

function down()
{
  down = true;
  t = setTimeout(function() { action(); }, 1500);
}

function cancel()
{
  down = false;
clearTimeout(t);
}

function action()
{
  if (!down)
    return;

  alert("Success!");
  down = false;
}

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..

bool down = false;
//your timeout var
var t;

function down()
{
  down = true;
  t = setTimeout(function() { action(); }, 1500);
}

function cancel()
{
  down = false;
clearTimeout(t);
}

function action()
{
  if (!down)
    return;

  alert("Success!");
  down = false;
}
夜光 2024-12-03 15:28:40

据我所知,您的代码有几个问题。

首先

bool down = false;

是无效的 JavaScript。应该是

var down = false;

然后你有两个名为 down 的变量:布尔值和函数。该函数将覆盖该变量,直到您执行将 down 设置为 true 或 false 的语句之一。

正如其他人所说:一旦设置,延迟函数将在1.5秒后继续执行,除非您取消超时。但话又说回来,这并不重要,因为您在执行任何操作之前都会检查鼠标按钮是否按下。

所以我建议将布尔变量重命名为 isMouseDown 或其他名称,然后重试。

There are several things wrong with your code as I see it.

First

bool down = false;

is not valid JavaScript. It should be

var down = false;

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 sets down 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.

似狗非友 2024-12-03 15:28:40

要取消 cancel() 函数中的超时,

var mytimer = null;

function ondown(){mytimer = setTimeOut('action()', 1500;)}   

function cancel(){clearTimeout(mytimer);}
function action(){mytimer=null; alert('success!');}

还请注意,您首先使用 down 作为变量 end,然后作为函数...调用 if(!down)总是返回 false,因为 down 引用了一个函数。

to cancel the timeout in the cancel() function, use

var mytimer = null;

function ondown(){mytimer = setTimeOut('action()', 1500;)}   

function cancel(){clearTimeout(mytimer);}
function action(){mytimer=null; alert('success!');}

also note that you used down first as a variable end then as a function... Calling if(!down) will always return false because down refers to a function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文