javascript动画:我需要嵌套的setInterval吗?

javascript动画:我需要嵌套的setInterval吗?,javascript,Javascript,我正在重新阐述我的问题,你可以在这里找到: 因为我认为我不清楚,解决方案也没有解决我的问题。 让我试着让ti更清楚: 假设我有一个动画animation(n),这是一个随机删除网页中每个单词的第一个字母的函数(我知道这很奇怪,但为了回答这个问题,这只是一个例子),我希望用户看到字母随随机时间消失(一系列动画、变速和暂停) 假定 function animation(n){ // n is the numer of letters to remove // code to remove the

我正在重新阐述我的问题,你可以在这里找到: 因为我认为我不清楚,解决方案也没有解决我的问题。 让我试着让ti更清楚:

假设我有一个动画
animation(n)
,这是一个随机删除网页中每个单词的第一个字母的函数(我知道这很奇怪,但为了回答这个问题,这只是一个例子),我希望用户看到字母随随机时间消失(一系列动画、变速和暂停)

假定

function animation(n){

// n is the numer of letters to remove
// code to remove the letter

}
如何每2秒触发一次此动画(),且动画应持续0.5秒

或随机方法:


如何每隔n(n>0&&n0&&n触发此动画(),我将使用如下的随机数生成器:

function animation(){
    //your code here
    setNewAnimation(); //set another call at random
}

function setNewAnimation() {
    setTimeout(animation, Math.floor((Math.random()*3)+1)*1000); //random between 1-3 seconds
}

window.onload = function() {
    setNewAnimation() //initial call
};