用JavaScript减轻效果 [英] Lightening effect with javascript
问题描述
我想在JavaScript中使用递归来产生无限循环.实际上,我希望不断给图像增光添彩.
I want to use recursion in JavaScript to produce an infinite loop. In fact, I desire to give an image a come-and-go effect, endlessly.
让我们先来看一些代码:
Let's take a look at some code first :
function lightening(){
$('#pic_holder').fadeOut(250).fadeIn(250);
setTimeout('lightening', 250);
}
此函数,按照其编写的内容,应该
This function, as it's written, should
- 应用
fadeOut(250)
和fadeIn(250)
效果; - 启用
setTimeout
函数,该函数又必须递归调用lightening
函数,此后重新应用[ fadeOut-fadeIn效果和 setTimeout ]块代码.
- apply the
fadeOut(250)
andfadeIn(250)
effects ; - engage the
setTimeout
function which in its turn must call recursively thelightening
function, henceforth re-applying the [fadeOut-fadeIn effect and setTimeout] block of code.
您会同意,这应该是无限制的,但事实并非如此.
This, you'll agree, should go ad infinitum, but it doesn't.
这是带有HTML的完整测试代码,您会注意到,它应用了fadeOut- fadeIn效果仅一次.
Here's the full test code, with HTML, as you can notice, it applies the fadeOut-fadeIn effect only once.
我在做什么错?
推荐答案
您真的应该做的是这样:
function lightening(){
$('#pic_holder').fadeOut(250).fadeIn(250, lightening);
}
这将使下一个循环在淡入完成时开始.将您自己的超时与jQuery动画调用暗示的超时混合在一起是棘手的,通常是不必要的.在您的情况下,您要在上一个周期的中间开始一个新的周期,直到250毫秒之后,它才真正生效.
That'll make the next cycle start when the fade-in has completed. Mixing your own timeouts with timeouts implied by jQuery animation calls is tricky and usually unnecessary. In your case, you're starting a new cycle halfway through a previous one, which really won't take effect until 250 milliseconds later.
这篇关于用JavaScript减轻效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!