jQuery 中的闪烁控制对象
我正在尝试使一个对象开始/停止闪烁(使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经创建了显式的无限递归。您正在调用
blink
,它又调用fadeOut
,后者又调用callback
,而callback
又调用blink
。我建议你用 setInterval 重写这个函数,比如:请注意,它没有经过测试,但至少它会给你一个方向。
更新:这是工作示例,但需要一些调试和时间跨度调整:
You have created explicit endless recursion. You are calling
blink
that callsfadeOut
that callscallback
that callsblink
. I would suggest you rewrite this function with setInterval, something like: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:
这就是我曾经做过类似的事情:
this is what i once used to do similar kind of thing: