jQuery函数animate with setTimeout只运行一次



我编写了一个函数,用于更改div中的文本并在延迟和几秒钟后更新文本。这个动画效果很好,但我想无休止地重复它。我已经尝试过添加setTimeout,但这只加载第一个变量的变化。谁能告诉我出了什么问题吗?

// After adding this the function loops once
setTimeout(function(){
transition_updated();
}, 10);
function transition_updated() {
// Set the ID where it's need to change the text
$("#post-change-transition").delay(2000).animate({opacity:0 },  function() {
var publishedText = $('.transition-published').html();
var updatedText = $('.transition-updated').html();
$(this).text(publishedText).animate({opacity:1},1000, function(){
$(this).delay(2000).animate({opacity:0},1000, function(){
$(this).text(updatedText).animate({opacity:1}, 1000, function(){});
});
});
});   
// Without this part the function loops three times
// After adding this part this will only loop once
setTimeout(function(){
transition_updated();
}, 10);
}
// Init function
transition_updated();
setTimeout(callback, ms);

ms是毫秒,也许它只是停止了?

你为什么不试试setInterval呢?

最新更新