清除超时不起作用

发布于 2024-10-26 19:51:50 字数 1089 浏览 4 评论 0原文

我正在尝试使用 Jquery 制作图像旋转器,但是当我将鼠标放在图像上时,我的旋转器不会停止,所以我猜测clearTimeout出了问题。
这是我的代码:

$(document).ready(function () {
    var o = 0
    var t = null;
    stop = false;
    $("img:gt(0)").hide();
    broj = ($("img").size());

    function promena() {
        o++;
        if (o == broj) {
            o = 0;
            $("img:lt(3)").hide(function () {
                $("img").eq(3).delay(1000).fadeOut(1000);
            });
        }
        $("img").eq(o).delay(1000).fadeIn(1000, function () {
            t = setTimeout(promena, 1000);
        });
    };

    t = setTimeout(promena, 1000);
    $("div img").mouseover(function () {
        clearTimeout(t);
    });
});

这是 HTML:

<html>
<head>
<title>M</title>
</head>
<body>
 <div>
 <img src="images/1.jpg"    />
 <img src="images/2.jpg"    />
 <img src="images/3.jpg"    />
 <img src="images/4.jpg"    />
 </div>
</body>
</html>

如果这意味着什么的话,我的所有图像都使用 img{position:absolute} 绝对定位

I'm trying to make image rotator with Jquery, but my rotator won't stop when I put the mouse over the images, so I'm guessing that something's wrong with clearTimeout.
Here's my code:

$(document).ready(function () {
    var o = 0
    var t = null;
    stop = false;
    $("img:gt(0)").hide();
    broj = ($("img").size());

    function promena() {
        o++;
        if (o == broj) {
            o = 0;
            $("img:lt(3)").hide(function () {
                $("img").eq(3).delay(1000).fadeOut(1000);
            });
        }
        $("img").eq(o).delay(1000).fadeIn(1000, function () {
            t = setTimeout(promena, 1000);
        });
    };

    t = setTimeout(promena, 1000);
    $("div img").mouseover(function () {
        clearTimeout(t);
    });
});

And here's HTML:

<html>
<head>
<title>M</title>
</head>
<body>
 <div>
 <img src="images/1.jpg"    />
 <img src="images/2.jpg"    />
 <img src="images/3.jpg"    />
 <img src="images/4.jpg"    />
 </div>
</body>
</html>

If it means anything, all of my images are positioned absolutely with img{position:absolute}

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

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

发布评论

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

评论(1

野心澎湃 2024-11-02 19:51:50

您的代码本质上是这样做的:

  1. 超时计时器运行一秒
  2. 延迟运行一秒
  3. 淡入淡出运行一秒
  4. 重复

-->如果您触发 mouseover 事件,并因此在 (1) 之外的任何点调用clearTimeout,则您不会清除任何正在运行的计时器。

换句话说,如果您在 1.5 秒后执行此操作,延迟仍在运行,然后逐渐淡出,然后您再次调用 setTimeout - 因此您尝试在调用 setTimeout 之前清除 Timeout。

您可能应该调用 .stop() 来停止延迟和淡入淡出队列,并防止 setTimeout 在它们完成时运行。

Your code is essentially doing this:

  1. timeout timer running for one second
  2. delay running for one second
  3. fade running for one second
  4. repeat

--> if you trigger the mouseover event, and hence call clearTimeout at any point other than (1), you aren't clearing any running timer.

In other words, if you do this after, lets say 1.5 seconds, the delay is still running, then the fade, and then you call setTimeout again - so you are attempting to clearTimeout before calling setTimeout.

You should probably call .stop() to stop the delay and fade queue, and prevent setTimeout running when they are complete.

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