即使在函数外部设置了 var,JS clearTimeout 也无法工作

发布于 2024-12-11 17:54:59 字数 734 浏览 0 评论 0原文

我试图在悬停时显示预览,但如果我将鼠标移开,我希望不显示预览。目前,如果我快速将鼠标移过“.searchRecord”元素,它无论如何都会在 300 毫秒后显示,并且会“卡住”,因为在 setTimeout 函数完成之前调用了鼠标关闭函数。如果我让光标停留在元素上,直到预览显示一切正常。

我在函数外部设置了变量,就像我在其他地方读到的那样,但是,它不会重置。我有点难住了。

var timer;
$('.searchRecord').hover(function() {
    $(this).children('.previewLoad').show();
    var current = '#'+$(this).children('div').attr('id');

    //slight delay before hover so they can select what they want
    var timer = window.setTimeout(function(){
        $(current).fadeIn('fast');
        $(current).siblings('.previewLoad').hide();
    }, 300);
}, function() {
    window.clearTimeout(timer);
    var current = '#'+$(this).children('div').attr('id');
    previewTimeouter(current);
});

I am trying to get a preview to display on hover, but if I move my mouse off, I want the preview not to display. Currently, if I quickly move my mouse across the ".searchRecord" element it will display after 300ms anyway, and be "stuck" as the mouse off function was called before the setTimeout function could finish. if I let me cursor stay on the element until the preview displays everything works just fine.

I set the variable outside the function as I read elsewhere, however, it doesn't reset. I'm a bit stumped.

var timer;
$('.searchRecord').hover(function() {
    $(this).children('.previewLoad').show();
    var current = '#'+$(this).children('div').attr('id');

    //slight delay before hover so they can select what they want
    var timer = window.setTimeout(function(){
        $(current).fadeIn('fast');
        $(current).siblings('.previewLoad').hide();
    }, 300);
}, function() {
    window.clearTimeout(timer);
    var current = '#'+$(this).children('div').attr('id');
    previewTimeouter(current);
});

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

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

发布评论

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

评论(2

岁月染过的梦 2024-12-18 17:54:59

您有重复的计时器声明。删除悬停回调中 vartimer 中的 var

var timer;
$('.searchRecord').hover(function() {
    $(this).children('.previewLoad').show();
    var current = '#'+$(this).children('div').attr('id');

    //slight delay before hover so they can select what they want
    timer = window.setTimeout(function(){
        $(current).fadeIn('fast');
        $(current).siblings('.previewLoad').hide();
    }, 300);
}, function() {
    window.clearTimeout(timer);
    var current = '#'+$(this).children('div').attr('id');
    previewTimeouter(current);
});

you have duplicated timer declaration. remove the var in var timer in the hover callback.

var timer;
$('.searchRecord').hover(function() {
    $(this).children('.previewLoad').show();
    var current = '#'+$(this).children('div').attr('id');

    //slight delay before hover so they can select what they want
    timer = window.setTimeout(function(){
        $(current).fadeIn('fast');
        $(current).siblings('.previewLoad').hide();
    }, 300);
}, function() {
    window.clearTimeout(timer);
    var current = '#'+$(this).children('div').attr('id');
    previewTimeouter(current);
});
倾城泪 2024-12-18 17:54:59

您在悬停回调中再次使用 var timer ,该回调将其范围限定为该函数并隐藏父 timer 变量。

删除该内部 var 一切都会好起来的。

You are using var timer again inside of the hover callback, which scopes it to that function and shadows the parent timer variable.

Remove that internal var and all shall be well.

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