用JavaScript减轻效果 [英] Lightening effect with javascript

查看:71
本文介绍了用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) and fadeIn(250) effects ;
  • engage the setTimeout function which in its turn must call recursively the lightening 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆