How to create infinite loop of Shuffle Text Effect using following code. It stops after animation is finished. How to start again when first loop of animation is completed?

<script src="http://code.jquery.com/jquery-1.6.3.min.js"></script>
<div id="container">text</div>

main script

    /**
 * @name        Shuffle Letters
 * @author      Martin Angelov
 * @version     1.0
 * @url         http://tutorialzine.com/2011/09/shuffle-letters-effect-jquery/
 * @license     MIT License
 */

(function($){

    $.fn.shuffleLetters = function(prop){

        var options = $.extend({
            "step"      : 10,           // How many times should the letters be changed
            "fps"       : 35,           // Frames Per Second
            "text"      : "",           // Use this text instead of the contents
            "callback"  : function(){}  // Run once the animation is complete
        },prop)

        return this.each(function(){

            var el = $(this),
                str = "";


            // Preventing parallel animations using a flag;

            if(el.data('animated')){
                return true;
            }

            el.data('animated',true);


            if(options.text) {
                str = options.text.split('');
            }
            else {
                str = el.text().split('');
            }

            // The types array holds the type for each character;
            // Letters holds the positions of non-space characters;

            var types = [],
                letters = [];

            // Looping through all the chars of the string

            for(var i=0;i<str.length;i++){

                var ch = str[i];

                if(ch == " "){
                    types[i] = "space";
                    continue;
                }
                else if(/[a-z]/.test(ch)){
                    types[i] = "lowerLetter";
                }
                else if(/[A-Z]/.test(ch)){
                    types[i] = "upperLetter";
                }
                else {
                    types[i] = "symbol";
                }

                letters.push(i);
            }

            el.html("");            

            // Self executing named function expression:

            (function shuffle(start){

                // This code is run options.fps times per second
                // and updates the contents of the page element

                var i,
                    len = letters.length, 
                    strCopy = str.slice(0); // Fresh copy of the string

                if(start>len){

                    // The animation is complete. Updating the
                    // flag and triggering the callback;

                    el.data('animated',false);
                    options.callback(el);
                    return;
                }

                // All the work gets done here
                for(i=Math.max(start,0); i < len; i++){

                    // The start argument and options.step limit
                    // the characters we will be working on at once

                    if( i < start+options.step){
                        // Generate a random character at thsi position
                        strCopy[letters[i]] = randomChar(types[letters[i]]);
                    }
                    else {
                        strCopy[letters[i]] = "";
                    }
                }

                el.text(strCopy.join(""));

                setTimeout(function(){

                    shuffle(start+1);

                },1000/options.fps);

            })(-options.step);


        });
    };

    function randomChar(type){
        var pool = "";

        if (type == "lowerLetter"){
            pool = "abcdefghijklmnopqrstuvwxyz0123456789";
        }
        else if (type == "upperLetter"){
            pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        else if (type == "symbol"){
            pool = ",.?/\\(^)![]{}*&^%$#'\"";
        }

        var arr = pool.split('');
        return arr[Math.floor(Math.random()*arr.length)];
    }

})(jQuery);

script

$(function(){

    // container is the DOM element;
    // userText is the textbox

    var container = $("#container")
        userText = $('#userText'); 

    // Shuffle the contents of container
    container.shuffleLetters();



    // Leave a 4 second pause

    setTimeout(function(){      
        // Shuffle the container with custom text
        container.shuffleLetters({
            "text": "Test it for yourself!"
        });
    },1000);

});

Dani AI

Generated

— the most reliable way to run the shuffle effect forever is to chain runs from the plugin's completion callback instead of trying to blindly poll with setInterval. 's idea (setInterval) will work, but if the interval is shorter than the animation runtime you can queue overlapping runs or miss frames. Chaining guarantees each new shuffle only starts once the previous one has finished and lets you insert a controlled pause.

A simple pattern (rotate an array of texts, pause between runs):

var texts = ["First phrase", "Another line", "Test it for yourself!"];
var i = 0;

(function loop(){
  $("#container").shuffleLetters({
    text: texts[i],
    callback: function(){
      setTimeout(loop, 800); // pause before next shuffle
    }
  });
  i = (i + 1) % texts.length;
})();

If you prefer setInterval, guard against overlap by checking the element state before starting a new run (clear the interval to stop looping). To pick the pause value accurately, estimate animation length as roughly (lettersLength + step) / fps seconds and add 100–300 ms buffer; using the callback avoids that guesswork. If a run never restarts, verify the plugin instance isn't left in an "animated" state or that you are passing a different text each time (same text may look like no-change).

I dont use jQuery at all, but from what I am seeing you can probably just change that setTimeout to a setInterval?

Just make sure that the interval length is the same as the amount of time it takes for the animation to finish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.