首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >setTimeout,clearTimeout jQuery

setTimeout,clearTimeout jQuery
EN

Stack Overflow用户
提问于 2011-06-18 00:27:28
回答 2查看 3.1K关注 0票数 1

我在一个内容滑块脚本中有一个现有的函数,它为下一张幻灯片设置动画的超时。我想在鼠标悬停时停止动画(我在脚本中添加了注释)。我做错了什么?谢谢你的帮助。

代码语言:javascript
复制
    function autoSlide() {
        if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
            if (currentPanel == panelCount) {
                var offset = 0;
                currentPanel = 1;
            } else {
                var offset = - (panelWidth*currentPanel);
                currentPanel += 1;
            };
            alterPanelHeight(currentPanel - 1);
            // Switch the current tab:
            slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a').addClass('current');
            // Slide:



            $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);

            setTimeout(autoSlide,settings.autoSlideInterval); 

     // this is my addition to try to stop the animation:               
            $('.panel-container', slider).mouseover(function(){
            //alert("hi");
               clearTimeout(autoSlide);

            }).mouseleave(function(){

               setTimeout(autoSlide,settings.autoSlideInterval);

            });
    // end of my addition          

        };
    };
EN

回答 2

Stack Overflow用户

发布于 2011-06-18 00:30:35

clearTimeout()使用返回值setTimeout() (操作的ID ),因此您应该在设置它时保存它,并将其用于清除。

代码语言:javascript
复制
var x=setTimeout(autoSlide,settings.autoSlideInterval); 
...
clearTimeout(x);

window.clearTimeout(timeoutID)

其中,window.setTimeout().返回的timeoutID是您希望清除的超时ID

票数 3
EN

Stack Overflow用户

发布于 2011-06-18 00:30:56

autoSlide不是超时,它是你的函数名,所以你不能清除它的超时。setTimeout返回对超时的引用,您需要捕获该超时并在超时上调用clearTimeout。请参见以下代码:

代码语言:javascript
复制
var slideTimeout = setTimeout(autoSlide,settings.autoSlideInterval); 


$('.panel-container', slider).mouseover(function(){
    clearTimeout(slideTimeout);
}).mouseleave(function(){
    slideTimeout = setTimeout(autoSlide,settings.autoSlideInterval);
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6388781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档