如何不结冰创造永恒的动画? [英] How do create perpetual animation without freezing?

查看:146
本文介绍了如何不结冰创造永恒的动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在JavaScript中的新手。我试着写一个程序,使图像fadeToggle所有的时间,所以我用了,而条件语句(一定变数相结合);运行和冻结我的电脑(然后记住,病毒基本上是无限的操作)后,我搜索放在这里了一下,意识到我也许应该,事实上,从来没有使用无限循环。

I am still a newbie at Javascript. I tried writing a program to make images fadeToggle all the time, so I used the while conditional statement (combined with a constant variable); after running that and freezing my computer (then remembering that viruses are basically infinite operations) I searched on here a bit, realized I should probably, indeed, never use infinite loops.

(实际上欲微光 - 这意味着从不透明0.9去0.6-0.9等...),所以其不一样fadeToggle,但仍保持了同样的问题;我怎么让它去无限?(不包括编写恶意code)

(Actually I want shimmer -- meaning to go from opacity 0.9 to 0.6 to 0.9 etc...) so its not the same as fadeToggle, but the same question remains; how do I make it go infinitely?(without writing malicious code.)

这是我写的,供大家参考:

This is what I wrote, for your reference:

<script>
$(document).ready(function(){
var always10 = 10;
while (always10 == 10) {
    $('.pimg').fadeToggle(2600);
                   }
});
</script>

另外,我在另一个线程,而(真)是无限循环更好的发现。所以另外一个问题:是无限循环的有时的确定

Also, I've found out on another thread that while(true) is better for infinite loops. So another question: are infinite loops sometimes OK?

推荐答案

由于Javascript asynchornous没有睡眠梅索德。但是相反,一个好主意是使用 的setInterval 的setTimeout

Since Javascript is asynchornous there is no sleep methode. But instead, a good idea is to use setInterval or setTimeout.

如果没有这些功能,你的循环是要采取大多数ressources,因为它可以,它可以导致冻结(取决于您的浏览器)。

Without these function, your loop is going to take as most ressources as it can and it can result in a freeze (depending on your browser).

这篇关于如何不结冰创造永恒的动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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