jQuery 中的闪烁控制对象

发布于 2024-11-29 07:20:35 字数 984 浏览 1 评论 0原文

我正在尝试使一个对象开始/停止闪烁(使用 jquery 中的对象的 fadeIn() 和 fadeOut() )。 我已经有一个方法眨眼(),使元素闪烁一次,并且它有效,但我试图让它再次闪烁,作为对 fadeOut() 的回调,并且似乎无法在没有得到堆栈溢出。这是我到目前为止所得到的:(

     Indicator = function(str) {
        this.el= $(str);    
        this.blink = function(){ 
            var callback = function() { 
                                return function(){
                                    console.log(this.el)
                                    this.blink();
                                }.apply(this);
                                //if (!this.stopped)
                                    //this.blink();
                            }.apply(this);


            this.el.fadeIn(200).delay(200).fadeOut(200,callback);
            }
            this.stopped = false;
            this.stop = function() { this.stopped = true; }
    }

    function start(){
       indicator =new Indicator('#indicator p');
       indicator.blink();
    }

我知道我的 apply() 是一团糟,抱歉)

I'm trying to make an object to start/stop blinking (with fadeIn() and fadeOut() for an object in jquery.
I already have a method blink() that makes the element blink once, and it works, but I'm trying to make it blink again as callback to the fadeOut() and don't seem to be able to make it without getting a stack overflow. This is what I've got so far:

     Indicator = function(str) {
        this.el= $(str);    
        this.blink = function(){ 
            var callback = function() { 
                                return function(){
                                    console.log(this.el)
                                    this.blink();
                                }.apply(this);
                                //if (!this.stopped)
                                    //this.blink();
                            }.apply(this);


            this.el.fadeIn(200).delay(200).fadeOut(200,callback);
            }
            this.stopped = false;
            this.stop = function() { this.stopped = true; }
    }

    function start(){
       indicator =new Indicator('#indicator p');
       indicator.blink();
    }

(I know my apply()'s are a mess, sorry)

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

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

发布评论

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

评论(2

花开柳相依 2024-12-06 07:20:35

您已经创建了显式的无限递归。您正在调用 blink,它又调用 fadeOut,后者又调用 callback,而 callback 又调用 blink。我建议你用 setInterval 重写这个函数,比如:

this.fadeDuration = 200;
this.blinkIntervalRef = null;
this.blink = function(){
    this.blinkIntervalRef = setInterval(
         function(){this.doBlink();},
         this.fadeDuration*3
    );
}
this.stop = function() {clearInterval(this.blinkIntervalRef );}
this.doBlink = function(){
    //this is just shortcut, not to make horizontal scroll
    var interval = this.blinkIntervalRef; 
    this.el.fadeIn(interval).delay(interval).fadeOut(interval);
}

请注意,它没有经过测试,但至少它会给你一个方向。

更新:这是工作示例,但需要一些调试和时间跨度调整:

Indicator = function(str) {
    this.el= $(str);    
    this.fadeDuration = 100;
    this.blinkIntervalRef = null;
    this.doBlink = function(){
        //this is just shortcut, not to make horizontal scroll
        var interval = this.blinkIntervalRef; 
        this.el.fadeIn(interval).delay(interval).fadeOut(interval);
    }
    this.blink = function(){
        var ctx = this;
        this.blinkIntervalRef = setInterval(function(){ctx.doBlink();},this.fadeDuration*4);
    }
    this.stop = function() {clearInterval(this.blinkIntervalRef);}
}

function start(){
   indicator = new Indicator('#indicator p');
   indicator.blink();
}

$(document).ready(function(){start();});

You have created explicit endless recursion. You are calling blink that calls fadeOut that calls callback that calls blink. I would suggest you rewrite this function with setInterval, something like:

this.fadeDuration = 200;
this.blinkIntervalRef = null;
this.blink = function(){
    this.blinkIntervalRef = setInterval(
         function(){this.doBlink();},
         this.fadeDuration*3
    );
}
this.stop = function() {clearInterval(this.blinkIntervalRef );}
this.doBlink = function(){
    //this is just shortcut, not to make horizontal scroll
    var interval = this.blinkIntervalRef; 
    this.el.fadeIn(interval).delay(interval).fadeOut(interval);
}

Note that it is not tested, but at least it will give you a direction.

Update: here's working example, but requires some debugging and time-spans adjustment:

Indicator = function(str) {
    this.el= $(str);    
    this.fadeDuration = 100;
    this.blinkIntervalRef = null;
    this.doBlink = function(){
        //this is just shortcut, not to make horizontal scroll
        var interval = this.blinkIntervalRef; 
        this.el.fadeIn(interval).delay(interval).fadeOut(interval);
    }
    this.blink = function(){
        var ctx = this;
        this.blinkIntervalRef = setInterval(function(){ctx.doBlink();},this.fadeDuration*4);
    }
    this.stop = function() {clearInterval(this.blinkIntervalRef);}
}

function start(){
   indicator = new Indicator('#indicator p');
   indicator.blink();
}

$(document).ready(function(){start();});
乜一 2024-12-06 07:20:35

这就是我曾经做过类似的事情:

function initializeBlink()
{      
        $('#indicator p').fadeToggle('slow', fadeToggleBlinker);
}

function fadeToggleBlinker()
{      
        var timeout = 2000;
        if(this.style.display == 'none')
                timeout = 1000;
        var tmp = $(this);
        window.setTimeout(function(){tmp.fadeToggle('slow', fadeToggleBlinker)}, timeout);
}

this is what i once used to do similar kind of thing:

function initializeBlink()
{      
        $('#indicator p').fadeToggle('slow', fadeToggleBlinker);
}

function fadeToggleBlinker()
{      
        var timeout = 2000;
        if(this.style.display == 'none')
                timeout = 1000;
        var tmp = $(this);
        window.setTimeout(function(){tmp.fadeToggle('slow', fadeToggleBlinker)}, timeout);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文