带有 setTimeout 的 jQuery 函数动画运行一次

时间:2021-01-24 12:03:08

标签: jquery jquery-animate settimeout

我制作了一个函数,可以更改 DIV 中的文本并在延迟几秒钟后更新文本。这个动画效果很好,但我想无休止地重复它。我已经尝试过添加 setTimeout 但这只会加载第一个 var 更改。有人可以告诉我出了什么问题吗?

// 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();

1 个答案:

答案 0 :(得分:0)

setTimeout(callback, ms);

ms 是毫秒,也许只是暂停?

为什么不试试setInterval