看不到 JS 中的问题

发布于 2024-09-12 17:41:16 字数 759 浏览 3 评论 0原文

我希望当鼠标悬停在图像上时,应该触发一次事件,并且只有在鼠标离开该图像并再次返回后,并且至少经过 2 秒后,才应该再次触发该事件。

如果我将鼠标悬停在图像上,它会像每毫秒一样被调用,并且根据我的函数逻辑,一旦您将鼠标悬停在变量“canhover”上,它就会变为 0,直到您将鼠标移出

此代码似乎有一个错误,我看不到它。我需要一双新的眼睛,但算法有点合乎逻辑

工作代码:

<script type="text/javascript">
var timeok = 1;
function redotimeok() {
    timeok = 1;
}
//
function onmenter()
{
if (timeok == 1) 
    {
  enter();
  timeok = 0;
    }
}
//
function onmleave()
{
  setTimeout(redotimeok, 2000);
  leave();
}
//

$('#cashrefresh').hover(onmenter,onmleave);

function enter(){
  $("#showname").load('./includes/do_name.inc.php');
  $("#cashrefresh").attr("src","images/reficonani.gif");
}

function leave(){
  $("#cashrefresh").attr("src","images/reficon.png");
}
</script>

I want that when mouse is over an image, an event should be triggered ONCE, and it should be triggered again only after mouse is out of that image and back again, and also at least 2 seconds passed.

If I leave my mouse over the image,it gets called like every milisecond,and by the logic of my function once you hover on the variable 'canhover' becomes 0 until you move mouse out

This code seems to have a bug and I cant see it. I need a new pair of eyes, but the algorithm is kinda logical

Working code :

<script type="text/javascript">
var timeok = 1;
function redotimeok() {
    timeok = 1;
}
//
function onmenter()
{
if (timeok == 1) 
    {
  enter();
  timeok = 0;
    }
}
//
function onmleave()
{
  setTimeout(redotimeok, 2000);
  leave();
}
//

$('#cashrefresh').hover(onmenter,onmleave);

function enter(){
  $("#showname").load('./includes/do_name.inc.php');
  $("#cashrefresh").attr("src","images/reficonani.gif");
}

function leave(){
  $("#cashrefresh").attr("src","images/reficon.png");
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

花开雨落又逢春i 2024-09-19 17:41:16

我不知道这是否能解决您的整个问题(因为我们没有详细说明它是什么),而是:

$('#cashrefresh').hover(onmenter(),onmleave());

尝试:

$('#cashrefresh').hover(onmenter,onmleave);

这里也是同样的事情:

setTimeout(redotimeok, 2000); // just the function name

另外,我不知道您曾经在哪里将 timeok 设置为零。您的意思是在 onmenter() 中设置 timeok = 0 吗?

I don't know if this will solve your entire problem (since we don't have a detailed description of what it is), but instead of:

$('#cashrefresh').hover(onmenter(),onmleave());

try:

$('#cashrefresh').hover(onmenter,onmleave);

And the same thing here:

setTimeout(redotimeok, 2000); // just the function name

Also, I don't see where you ever set timeok to zero. Do you mean to set timeok = 0 in onmenter()?

夜司空 2024-09-19 17:41:16

jquery 有两种方法可以解决您的问题:

.mouseenter().mouseleave()

查看那里的演示。

编辑:

我认为hover适用于mouseovermouseout,抱歉造成混淆。

我又检查了你的代码。当鼠标悬停在图像上时,您似乎正在更改图像,这会强制浏览器加载新图像,并且旧图像会消失一小会儿,直到新图像出现,我认为这必须连续触发两个处理程序你就会得到这种行为。

尽量不要更改图像的来源,注释掉该行,然后使用 console.log("some message") 并查看该消息是否与 .load() 一样重复 之前被解雇过。

希望这有帮助。

There are two methods in jquery for your problem:

.mouseenter() and .mouseleave()

Check out the demos there.

EDIT:

I thought hover was for mouseover and mouseout, sorry for confusion.

I checked your code again. And it seems that you're changing the image when mouse gets over the image, which forces browser to load the new image and the old image disappears for a very little while till the new one appears and i think this must be triggering both handlers continuosly and you're getting this behaviour.

Try not to change the source of the image, comment out that line and instead console.log("some message") there and see if the message is repeated as much as .load() was fired before.

Hope this helps.

坠似风落 2024-09-19 17:41:16

尝试更改 onmleave 函数,如下所示:

   function onmleave()
    {
      setTimeout(redotimeok, 2000);
      leave();
    }

Try changing onmleave function as follows:

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