【问题标题】:jQuery function animate with setTimeout is running once带有 setTimeout 的 jQuery 函数动画运行一次
【发布时间】:2021-04-28 09:42:28
【问题描述】:

我创建了一个函数来更改 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();

【问题讨论】:

    标签: jquery jquery-animate settimeout


    【解决方案1】:
    setTimeout(callback, ms);
    

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

    你为什么不试试setInterval

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 2011-12-13
      • 2017-01-24
      相关资源
      最近更新 更多