Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

jQuery function animate with setTimeout is running once

I made a function that change the text inside a DIV and update the text after delay and few seconds. This animation works nice but i want to repeat it endless. I have tried this with adding setTimeout but this only loads the first var change. Can somebody advice me what is going wrong?

// 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();
question from:https://stackoverflow.com/questions/65870302/jquery-function-animate-with-settimeout-is-running-once

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
setTimeout(callback, ms);

ms is milliseconds, maybe it is just halted?

and why don't you try setInterval?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...