How to repeat Type Writer effect using JS?












2















I want to have a type writer effect on my website. Such that it repeats the data in array infinite times not just once.



I tried to build one with JS but it's only showing data in the array only one time.



For example I have an array :



var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];


I want it to be shown like this :



Amsterdam
Full Service.
Webdevelopment
Vivank

Amsterdam
Full Service.
Webdevelopment
Vivank

Amsterdam
Full Service.
Webdevelopment
Vivank.....



upto many times




What my code is doing it stops after one cycle.
I want it to repeat cycle unlimited times.



Also, I am getting some kind of error.




Error:
{ "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 }




Any Help?



Also how to add a pause to animation so that it starts to alter the text in p after a minute ?



Here is what i have tried so far :






document.addEventListener('DOMContentLoaded',function(event){
// array with texts to type in typewriter
var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

// type one text in the typwriter
// keeps calling itself until the text is finished
function typeWriter(text, i, fnCallback) {
// chekc if text isn't finished yet
if (i < (text.length)) {
// add next character to h1
document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

// wait for a while and call this function again for next character
setTimeout(function() {
typeWriter(text, i + 1, fnCallback)
}, 100);
}
// text finished, call callback if there is a callback function
else if (typeof fnCallback == 'function') {
// call callback after timeout
setTimeout(fnCallback, 700);
}
}
// start a typewriter animation for a text in the dataText array
function StartTextAnimation(i) {
if (typeof dataText[i] == 'undefined'){
setTimeout(function() {
StartTextAnimation(0);
}, 20000);
}
// check if dataText[i] exists
if (i < dataText[i].length) {
// text exists! start typewriter animation
typeWriter(dataText[i], 0, function(){
// after callback (and whole text has been animated), start next text
StartTextAnimation(i + 1);
});
}

}
// start the text animation
StartTextAnimation(0);
});

<p id="tw">A</p>












share|improve this question





























    2















    I want to have a type writer effect on my website. Such that it repeats the data in array infinite times not just once.



    I tried to build one with JS but it's only showing data in the array only one time.



    For example I have an array :



    var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];


    I want it to be shown like this :



    Amsterdam
    Full Service.
    Webdevelopment
    Vivank

    Amsterdam
    Full Service.
    Webdevelopment
    Vivank

    Amsterdam
    Full Service.
    Webdevelopment
    Vivank.....



    upto many times




    What my code is doing it stops after one cycle.
    I want it to repeat cycle unlimited times.



    Also, I am getting some kind of error.




    Error:
    { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 }




    Any Help?



    Also how to add a pause to animation so that it starts to alter the text in p after a minute ?



    Here is what i have tried so far :






    document.addEventListener('DOMContentLoaded',function(event){
    // array with texts to type in typewriter
    var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

    // type one text in the typwriter
    // keeps calling itself until the text is finished
    function typeWriter(text, i, fnCallback) {
    // chekc if text isn't finished yet
    if (i < (text.length)) {
    // add next character to h1
    document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

    // wait for a while and call this function again for next character
    setTimeout(function() {
    typeWriter(text, i + 1, fnCallback)
    }, 100);
    }
    // text finished, call callback if there is a callback function
    else if (typeof fnCallback == 'function') {
    // call callback after timeout
    setTimeout(fnCallback, 700);
    }
    }
    // start a typewriter animation for a text in the dataText array
    function StartTextAnimation(i) {
    if (typeof dataText[i] == 'undefined'){
    setTimeout(function() {
    StartTextAnimation(0);
    }, 20000);
    }
    // check if dataText[i] exists
    if (i < dataText[i].length) {
    // text exists! start typewriter animation
    typeWriter(dataText[i], 0, function(){
    // after callback (and whole text has been animated), start next text
    StartTextAnimation(i + 1);
    });
    }

    }
    // start the text animation
    StartTextAnimation(0);
    });

    <p id="tw">A</p>












    share|improve this question



























      2












      2








      2








      I want to have a type writer effect on my website. Such that it repeats the data in array infinite times not just once.



      I tried to build one with JS but it's only showing data in the array only one time.



      For example I have an array :



      var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];


      I want it to be shown like this :



      Amsterdam
      Full Service.
      Webdevelopment
      Vivank

      Amsterdam
      Full Service.
      Webdevelopment
      Vivank

      Amsterdam
      Full Service.
      Webdevelopment
      Vivank.....



      upto many times




      What my code is doing it stops after one cycle.
      I want it to repeat cycle unlimited times.



      Also, I am getting some kind of error.




      Error:
      { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 }




      Any Help?



      Also how to add a pause to animation so that it starts to alter the text in p after a minute ?



      Here is what i have tried so far :






      document.addEventListener('DOMContentLoaded',function(event){
      // array with texts to type in typewriter
      var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

      // type one text in the typwriter
      // keeps calling itself until the text is finished
      function typeWriter(text, i, fnCallback) {
      // chekc if text isn't finished yet
      if (i < (text.length)) {
      // add next character to h1
      document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

      // wait for a while and call this function again for next character
      setTimeout(function() {
      typeWriter(text, i + 1, fnCallback)
      }, 100);
      }
      // text finished, call callback if there is a callback function
      else if (typeof fnCallback == 'function') {
      // call callback after timeout
      setTimeout(fnCallback, 700);
      }
      }
      // start a typewriter animation for a text in the dataText array
      function StartTextAnimation(i) {
      if (typeof dataText[i] == 'undefined'){
      setTimeout(function() {
      StartTextAnimation(0);
      }, 20000);
      }
      // check if dataText[i] exists
      if (i < dataText[i].length) {
      // text exists! start typewriter animation
      typeWriter(dataText[i], 0, function(){
      // after callback (and whole text has been animated), start next text
      StartTextAnimation(i + 1);
      });
      }

      }
      // start the text animation
      StartTextAnimation(0);
      });

      <p id="tw">A</p>












      share|improve this question
















      I want to have a type writer effect on my website. Such that it repeats the data in array infinite times not just once.



      I tried to build one with JS but it's only showing data in the array only one time.



      For example I have an array :



      var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];


      I want it to be shown like this :



      Amsterdam
      Full Service.
      Webdevelopment
      Vivank

      Amsterdam
      Full Service.
      Webdevelopment
      Vivank

      Amsterdam
      Full Service.
      Webdevelopment
      Vivank.....



      upto many times




      What my code is doing it stops after one cycle.
      I want it to repeat cycle unlimited times.



      Also, I am getting some kind of error.




      Error:
      { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 }




      Any Help?



      Also how to add a pause to animation so that it starts to alter the text in p after a minute ?



      Here is what i have tried so far :






      document.addEventListener('DOMContentLoaded',function(event){
      // array with texts to type in typewriter
      var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

      // type one text in the typwriter
      // keeps calling itself until the text is finished
      function typeWriter(text, i, fnCallback) {
      // chekc if text isn't finished yet
      if (i < (text.length)) {
      // add next character to h1
      document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

      // wait for a while and call this function again for next character
      setTimeout(function() {
      typeWriter(text, i + 1, fnCallback)
      }, 100);
      }
      // text finished, call callback if there is a callback function
      else if (typeof fnCallback == 'function') {
      // call callback after timeout
      setTimeout(fnCallback, 700);
      }
      }
      // start a typewriter animation for a text in the dataText array
      function StartTextAnimation(i) {
      if (typeof dataText[i] == 'undefined'){
      setTimeout(function() {
      StartTextAnimation(0);
      }, 20000);
      }
      // check if dataText[i] exists
      if (i < dataText[i].length) {
      // text exists! start typewriter animation
      typeWriter(dataText[i], 0, function(){
      // after callback (and whole text has been animated), start next text
      StartTextAnimation(i + 1);
      });
      }

      }
      // start the text animation
      StartTextAnimation(0);
      });

      <p id="tw">A</p>








      document.addEventListener('DOMContentLoaded',function(event){
      // array with texts to type in typewriter
      var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

      // type one text in the typwriter
      // keeps calling itself until the text is finished
      function typeWriter(text, i, fnCallback) {
      // chekc if text isn't finished yet
      if (i < (text.length)) {
      // add next character to h1
      document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

      // wait for a while and call this function again for next character
      setTimeout(function() {
      typeWriter(text, i + 1, fnCallback)
      }, 100);
      }
      // text finished, call callback if there is a callback function
      else if (typeof fnCallback == 'function') {
      // call callback after timeout
      setTimeout(fnCallback, 700);
      }
      }
      // start a typewriter animation for a text in the dataText array
      function StartTextAnimation(i) {
      if (typeof dataText[i] == 'undefined'){
      setTimeout(function() {
      StartTextAnimation(0);
      }, 20000);
      }
      // check if dataText[i] exists
      if (i < dataText[i].length) {
      // text exists! start typewriter animation
      typeWriter(dataText[i], 0, function(){
      // after callback (and whole text has been animated), start next text
      StartTextAnimation(i + 1);
      });
      }

      }
      // start the text animation
      StartTextAnimation(0);
      });

      <p id="tw">A</p>





      document.addEventListener('DOMContentLoaded',function(event){
      // array with texts to type in typewriter
      var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

      // type one text in the typwriter
      // keeps calling itself until the text is finished
      function typeWriter(text, i, fnCallback) {
      // chekc if text isn't finished yet
      if (i < (text.length)) {
      // add next character to h1
      document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

      // wait for a while and call this function again for next character
      setTimeout(function() {
      typeWriter(text, i + 1, fnCallback)
      }, 100);
      }
      // text finished, call callback if there is a callback function
      else if (typeof fnCallback == 'function') {
      // call callback after timeout
      setTimeout(fnCallback, 700);
      }
      }
      // start a typewriter animation for a text in the dataText array
      function StartTextAnimation(i) {
      if (typeof dataText[i] == 'undefined'){
      setTimeout(function() {
      StartTextAnimation(0);
      }, 20000);
      }
      // check if dataText[i] exists
      if (i < dataText[i].length) {
      // text exists! start typewriter animation
      typeWriter(dataText[i], 0, function(){
      // after callback (and whole text has been animated), start next text
      StartTextAnimation(i + 1);
      });
      }

      }
      // start the text animation
      StartTextAnimation(0);
      });

      <p id="tw">A</p>






      javascript jquery html






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 4:31







      Mansi Shukla

















      asked Jan 2 at 4:18









      Mansi ShuklaMansi Shukla

      9512




      9512
























          2 Answers
          2






          active

          oldest

          votes


















          2














          All you need to do is pass the i (the index of the item you're iterating over) modulo dataText.length, to ensure that once i reaches dataText.length, StartTextAnimation gets called with 0 rather than an index which doesn't exist:



          StartTextAnimation((i + 1) % dataText.length);





          document.addEventListener('DOMContentLoaded',function(event){
          // array with texts to type in typewriter
          var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

          // type one text in the typwriter
          // keeps calling itself until the text is finished
          function typeWriter(text, i, fnCallback) {
          // chekc if text isn't finished yet
          if (i < (text.length)) {
          // add next character to h1
          document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

          // wait for a while and call this function again for next character
          setTimeout(function() {
          typeWriter(text, i + 1, fnCallback)
          }, 100);
          }
          // text finished, call callback if there is a callback function
          else if (typeof fnCallback == 'function') {
          // call callback after timeout
          setTimeout(fnCallback, 700);
          }
          }
          // start a typewriter animation for a text in the dataText array
          function StartTextAnimation(i) {
          if (typeof dataText[i] == 'undefined'){
          setTimeout(function() {
          StartTextAnimation(0);
          }, 20000);
          }
          // check if dataText[i] exists
          if (i < dataText[i].length) {
          // text exists! start typewriter animation
          typeWriter(dataText[i], 0, function(){
          // after callback (and whole text has been animated), start next text
          StartTextAnimation((i + 1) % dataText.length);
          });
          }

          }
          // start the text animation
          StartTextAnimation(0);
          });

          <p id="tw">A</p>





          Or, for a large delay after a full loop is done, use else if after the undefined check instead of if:



          if (dataText[i] === undefined) {
          setTimeout(function() {
          StartTextAnimation(0);
          }, 20000);
          } else if (i < dataText[i].length) {
          // text exists! start typewriter animation
          typeWriter(dataText[i], 0, function() {
          // after callback (and whole text has been animated), start next text
          StartTextAnimation(i + 1);
          });
          }





          document.addEventListener('DOMContentLoaded', function(event) {
          // array with texts to type in typewriter
          var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

          // type one text in the typwriter
          // keeps calling itself until the text is finished
          function typeWriter(text, i, fnCallback) {
          // chekc if text isn't finished yet
          if (i < (text.length)) {
          // add next character to h1
          document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

          // wait for a while and call this function again for next character
          setTimeout(function() {
          typeWriter(text, i + 1, fnCallback)
          }, 100);
          }
          // text finished, call callback if there is a callback function
          else if (typeof fnCallback == 'function') {
          // call callback after timeout
          setTimeout(fnCallback, 700);
          }
          }
          // start a typewriter animation for a text in the dataText array
          function StartTextAnimation(i) {
          if (dataText[i] === undefined) {
          setTimeout(function() {
          StartTextAnimation(0);
          }, 20000);
          } else if (i < dataText[i].length) {
          // text exists! start typewriter animation
          typeWriter(dataText[i], 0, function() {
          // after callback (and whole text has been animated), start next text
          StartTextAnimation(i + 1);
          });
          }
          }
          // start the text animation
          StartTextAnimation(0);
          });

          <p id="tw">A</p>








          share|improve this answer


























          • Thanks. That's what I was missing

            – Mansi Shukla
            Jan 2 at 4:24











          • Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

            – Mansi Shukla
            Jan 2 at 4:27













          • See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

            – CertainPerformance
            Jan 2 at 4:34











          • What about the pause ?

            – Mansi Shukla
            Jan 2 at 4:35











          • That's what the 20000 timeout is for. Set it to however many ms you want

            – CertainPerformance
            Jan 2 at 4:36



















          1














          You already have the condition to check for undefined index, but you missed the additional condition to restart your sequence.



          Also, you have some poor choice of variable names, namely, index "i" in both functions can be confusing, as one function iterates over words in array, and the other one over characters.



          I've rewritten your functions and make the entire code more production-friendly and human-readable of what-is-going-on:






          // array with text to type in typewriter
          var dataText = [
          "Web Design.",
          "Web Development.",
          "Web Programming."
          ];

          // typewriter speed
          // set delay time between each character typing time
          var CharDelay = 50;

          // pause time between each completed word (delay before next word starts)
          var WordPause = 1000;

          // set initial word in dataText array
          var WordOffset = 0;

          // set sequence restart interval N [ms]
          var RestartInterval = 3000;

          // type one text in the typewriter
          // keeps calling itself until complete word is printed
          function typeWriter(text, i, fnCallback) {
          // check if word isn't finished yet
          if (i < (text.length)) {
          // add next character to html
          document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

          // wait for a while and call this function again for next character
          setTimeout(function() {
          typeWriter(text, i+1, fnCallback)
          }, CharDelay);
          }

          // text finished, call callback if there is a callback function
          else if (typeof fnCallback == 'function') {
          // call callback after timeout
          setTimeout(fnCallback, WordPause);
          }
          }

          // start a typewriter animation in the dataText array
          // @param int j = dataText array word index
          function StartTextAnimation(j) {
          //console.log(j);
          //console.log(dataText.length);
          // restart animation after N seconds
          if (typeof dataText[j] == 'undefined' || j == dataText.length) {
          setTimeout(function() {
          StartTextAnimation(WordOffset);
          }, RestartInterval);
          }

          // check if dataText[j] exists
          else if (j < dataText[j].length) {
          // text exists! start typewriter animation
          typeWriter(dataText[j], 0, function() {
          // after callback (and whole text has been animated), start next word
          StartTextAnimation((j+1));
          });
          }
          }

          document.addEventListener('DOMContentLoaded', function(event) {
          // start text animation
          StartTextAnimation(WordOffset);
          });

          <div id="typewriter"></div>








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001139%2fhow-to-repeat-type-writer-effect-using-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            All you need to do is pass the i (the index of the item you're iterating over) modulo dataText.length, to ensure that once i reaches dataText.length, StartTextAnimation gets called with 0 rather than an index which doesn't exist:



            StartTextAnimation((i + 1) % dataText.length);





            document.addEventListener('DOMContentLoaded',function(event){
            // array with texts to type in typewriter
            var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (typeof dataText[i] == 'undefined'){
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            }
            // check if dataText[i] exists
            if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function(){
            // after callback (and whole text has been animated), start next text
            StartTextAnimation((i + 1) % dataText.length);
            });
            }

            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            Or, for a large delay after a full loop is done, use else if after the undefined check instead of if:



            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }





            document.addEventListener('DOMContentLoaded', function(event) {
            // array with texts to type in typewriter
            var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }
            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>








            share|improve this answer


























            • Thanks. That's what I was missing

              – Mansi Shukla
              Jan 2 at 4:24











            • Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

              – Mansi Shukla
              Jan 2 at 4:27













            • See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

              – CertainPerformance
              Jan 2 at 4:34











            • What about the pause ?

              – Mansi Shukla
              Jan 2 at 4:35











            • That's what the 20000 timeout is for. Set it to however many ms you want

              – CertainPerformance
              Jan 2 at 4:36
















            2














            All you need to do is pass the i (the index of the item you're iterating over) modulo dataText.length, to ensure that once i reaches dataText.length, StartTextAnimation gets called with 0 rather than an index which doesn't exist:



            StartTextAnimation((i + 1) % dataText.length);





            document.addEventListener('DOMContentLoaded',function(event){
            // array with texts to type in typewriter
            var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (typeof dataText[i] == 'undefined'){
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            }
            // check if dataText[i] exists
            if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function(){
            // after callback (and whole text has been animated), start next text
            StartTextAnimation((i + 1) % dataText.length);
            });
            }

            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            Or, for a large delay after a full loop is done, use else if after the undefined check instead of if:



            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }





            document.addEventListener('DOMContentLoaded', function(event) {
            // array with texts to type in typewriter
            var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }
            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>








            share|improve this answer


























            • Thanks. That's what I was missing

              – Mansi Shukla
              Jan 2 at 4:24











            • Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

              – Mansi Shukla
              Jan 2 at 4:27













            • See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

              – CertainPerformance
              Jan 2 at 4:34











            • What about the pause ?

              – Mansi Shukla
              Jan 2 at 4:35











            • That's what the 20000 timeout is for. Set it to however many ms you want

              – CertainPerformance
              Jan 2 at 4:36














            2












            2








            2







            All you need to do is pass the i (the index of the item you're iterating over) modulo dataText.length, to ensure that once i reaches dataText.length, StartTextAnimation gets called with 0 rather than an index which doesn't exist:



            StartTextAnimation((i + 1) % dataText.length);





            document.addEventListener('DOMContentLoaded',function(event){
            // array with texts to type in typewriter
            var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (typeof dataText[i] == 'undefined'){
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            }
            // check if dataText[i] exists
            if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function(){
            // after callback (and whole text has been animated), start next text
            StartTextAnimation((i + 1) % dataText.length);
            });
            }

            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            Or, for a large delay after a full loop is done, use else if after the undefined check instead of if:



            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }





            document.addEventListener('DOMContentLoaded', function(event) {
            // array with texts to type in typewriter
            var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }
            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>








            share|improve this answer















            All you need to do is pass the i (the index of the item you're iterating over) modulo dataText.length, to ensure that once i reaches dataText.length, StartTextAnimation gets called with 0 rather than an index which doesn't exist:



            StartTextAnimation((i + 1) % dataText.length);





            document.addEventListener('DOMContentLoaded',function(event){
            // array with texts to type in typewriter
            var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (typeof dataText[i] == 'undefined'){
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            }
            // check if dataText[i] exists
            if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function(){
            // after callback (and whole text has been animated), start next text
            StartTextAnimation((i + 1) % dataText.length);
            });
            }

            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            Or, for a large delay after a full loop is done, use else if after the undefined check instead of if:



            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }





            document.addEventListener('DOMContentLoaded', function(event) {
            // array with texts to type in typewriter
            var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }
            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>








            document.addEventListener('DOMContentLoaded',function(event){
            // array with texts to type in typewriter
            var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (typeof dataText[i] == 'undefined'){
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            }
            // check if dataText[i] exists
            if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function(){
            // after callback (and whole text has been animated), start next text
            StartTextAnimation((i + 1) % dataText.length);
            });
            }

            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            document.addEventListener('DOMContentLoaded',function(event){
            // array with texts to type in typewriter
            var dataText = [ "Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i+1) +'<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (typeof dataText[i] == 'undefined'){
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            }
            // check if dataText[i] exists
            if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function(){
            // after callback (and whole text has been animated), start next text
            StartTextAnimation((i + 1) % dataText.length);
            });
            }

            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            document.addEventListener('DOMContentLoaded', function(event) {
            // array with texts to type in typewriter
            var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }
            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>





            document.addEventListener('DOMContentLoaded', function(event) {
            // array with texts to type in typewriter
            var dataText = ["Amsterdam.", "Full Service.", "Webdevelopment.", "Vivank"];

            // type one text in the typwriter
            // keeps calling itself until the text is finished
            function typeWriter(text, i, fnCallback) {
            // chekc if text isn't finished yet
            if (i < (text.length)) {
            // add next character to h1
            document.querySelector("#tw").innerHTML = text.substring(0, i + 1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i + 1, fnCallback)
            }, 100);
            }
            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, 700);
            }
            }
            // start a typewriter animation for a text in the dataText array
            function StartTextAnimation(i) {
            if (dataText[i] === undefined) {
            setTimeout(function() {
            StartTextAnimation(0);
            }, 20000);
            } else if (i < dataText[i].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[i], 0, function() {
            // after callback (and whole text has been animated), start next text
            StartTextAnimation(i + 1);
            });
            }
            }
            // start the text animation
            StartTextAnimation(0);
            });

            <p id="tw">A</p>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 2 at 4:32

























            answered Jan 2 at 4:22









            CertainPerformanceCertainPerformance

            91k165179




            91k165179













            • Thanks. That's what I was missing

              – Mansi Shukla
              Jan 2 at 4:24











            • Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

              – Mansi Shukla
              Jan 2 at 4:27













            • See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

              – CertainPerformance
              Jan 2 at 4:34











            • What about the pause ?

              – Mansi Shukla
              Jan 2 at 4:35











            • That's what the 20000 timeout is for. Set it to however many ms you want

              – CertainPerformance
              Jan 2 at 4:36



















            • Thanks. That's what I was missing

              – Mansi Shukla
              Jan 2 at 4:24











            • Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

              – Mansi Shukla
              Jan 2 at 4:27













            • See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

              – CertainPerformance
              Jan 2 at 4:34











            • What about the pause ?

              – Mansi Shukla
              Jan 2 at 4:35











            • That's what the 20000 timeout is for. Set it to however many ms you want

              – CertainPerformance
              Jan 2 at 4:36

















            Thanks. That's what I was missing

            – Mansi Shukla
            Jan 2 at 4:24





            Thanks. That's what I was missing

            – Mansi Shukla
            Jan 2 at 4:24













            Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

            – Mansi Shukla
            Jan 2 at 4:27







            Can you suggest a pause like after some time it starts to alter the text in p like after a minute or two? and what about that error ?

            – Mansi Shukla
            Jan 2 at 4:27















            See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

            – CertainPerformance
            Jan 2 at 4:34





            See edit, use else if instead of if. your Error: { "message": "Script error.", "filename": "", "lineno": 0, "colno": 0 } doesn't appear to occur in the code in the question - rather, it's "message": "Uncaught TypeError: Cannot read property 'length' of undefined"

            – CertainPerformance
            Jan 2 at 4:34













            What about the pause ?

            – Mansi Shukla
            Jan 2 at 4:35





            What about the pause ?

            – Mansi Shukla
            Jan 2 at 4:35













            That's what the 20000 timeout is for. Set it to however many ms you want

            – CertainPerformance
            Jan 2 at 4:36





            That's what the 20000 timeout is for. Set it to however many ms you want

            – CertainPerformance
            Jan 2 at 4:36













            1














            You already have the condition to check for undefined index, but you missed the additional condition to restart your sequence.



            Also, you have some poor choice of variable names, namely, index "i" in both functions can be confusing, as one function iterates over words in array, and the other one over characters.



            I've rewritten your functions and make the entire code more production-friendly and human-readable of what-is-going-on:






            // array with text to type in typewriter
            var dataText = [
            "Web Design.",
            "Web Development.",
            "Web Programming."
            ];

            // typewriter speed
            // set delay time between each character typing time
            var CharDelay = 50;

            // pause time between each completed word (delay before next word starts)
            var WordPause = 1000;

            // set initial word in dataText array
            var WordOffset = 0;

            // set sequence restart interval N [ms]
            var RestartInterval = 3000;

            // type one text in the typewriter
            // keeps calling itself until complete word is printed
            function typeWriter(text, i, fnCallback) {
            // check if word isn't finished yet
            if (i < (text.length)) {
            // add next character to html
            document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

            // wait for a while and call this function again for next character
            setTimeout(function() {
            typeWriter(text, i+1, fnCallback)
            }, CharDelay);
            }

            // text finished, call callback if there is a callback function
            else if (typeof fnCallback == 'function') {
            // call callback after timeout
            setTimeout(fnCallback, WordPause);
            }
            }

            // start a typewriter animation in the dataText array
            // @param int j = dataText array word index
            function StartTextAnimation(j) {
            //console.log(j);
            //console.log(dataText.length);
            // restart animation after N seconds
            if (typeof dataText[j] == 'undefined' || j == dataText.length) {
            setTimeout(function() {
            StartTextAnimation(WordOffset);
            }, RestartInterval);
            }

            // check if dataText[j] exists
            else if (j < dataText[j].length) {
            // text exists! start typewriter animation
            typeWriter(dataText[j], 0, function() {
            // after callback (and whole text has been animated), start next word
            StartTextAnimation((j+1));
            });
            }
            }

            document.addEventListener('DOMContentLoaded', function(event) {
            // start text animation
            StartTextAnimation(WordOffset);
            });

            <div id="typewriter"></div>








            share|improve this answer




























              1














              You already have the condition to check for undefined index, but you missed the additional condition to restart your sequence.



              Also, you have some poor choice of variable names, namely, index "i" in both functions can be confusing, as one function iterates over words in array, and the other one over characters.



              I've rewritten your functions and make the entire code more production-friendly and human-readable of what-is-going-on:






              // array with text to type in typewriter
              var dataText = [
              "Web Design.",
              "Web Development.",
              "Web Programming."
              ];

              // typewriter speed
              // set delay time between each character typing time
              var CharDelay = 50;

              // pause time between each completed word (delay before next word starts)
              var WordPause = 1000;

              // set initial word in dataText array
              var WordOffset = 0;

              // set sequence restart interval N [ms]
              var RestartInterval = 3000;

              // type one text in the typewriter
              // keeps calling itself until complete word is printed
              function typeWriter(text, i, fnCallback) {
              // check if word isn't finished yet
              if (i < (text.length)) {
              // add next character to html
              document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

              // wait for a while and call this function again for next character
              setTimeout(function() {
              typeWriter(text, i+1, fnCallback)
              }, CharDelay);
              }

              // text finished, call callback if there is a callback function
              else if (typeof fnCallback == 'function') {
              // call callback after timeout
              setTimeout(fnCallback, WordPause);
              }
              }

              // start a typewriter animation in the dataText array
              // @param int j = dataText array word index
              function StartTextAnimation(j) {
              //console.log(j);
              //console.log(dataText.length);
              // restart animation after N seconds
              if (typeof dataText[j] == 'undefined' || j == dataText.length) {
              setTimeout(function() {
              StartTextAnimation(WordOffset);
              }, RestartInterval);
              }

              // check if dataText[j] exists
              else if (j < dataText[j].length) {
              // text exists! start typewriter animation
              typeWriter(dataText[j], 0, function() {
              // after callback (and whole text has been animated), start next word
              StartTextAnimation((j+1));
              });
              }
              }

              document.addEventListener('DOMContentLoaded', function(event) {
              // start text animation
              StartTextAnimation(WordOffset);
              });

              <div id="typewriter"></div>








              share|improve this answer


























                1












                1








                1







                You already have the condition to check for undefined index, but you missed the additional condition to restart your sequence.



                Also, you have some poor choice of variable names, namely, index "i" in both functions can be confusing, as one function iterates over words in array, and the other one over characters.



                I've rewritten your functions and make the entire code more production-friendly and human-readable of what-is-going-on:






                // array with text to type in typewriter
                var dataText = [
                "Web Design.",
                "Web Development.",
                "Web Programming."
                ];

                // typewriter speed
                // set delay time between each character typing time
                var CharDelay = 50;

                // pause time between each completed word (delay before next word starts)
                var WordPause = 1000;

                // set initial word in dataText array
                var WordOffset = 0;

                // set sequence restart interval N [ms]
                var RestartInterval = 3000;

                // type one text in the typewriter
                // keeps calling itself until complete word is printed
                function typeWriter(text, i, fnCallback) {
                // check if word isn't finished yet
                if (i < (text.length)) {
                // add next character to html
                document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

                // wait for a while and call this function again for next character
                setTimeout(function() {
                typeWriter(text, i+1, fnCallback)
                }, CharDelay);
                }

                // text finished, call callback if there is a callback function
                else if (typeof fnCallback == 'function') {
                // call callback after timeout
                setTimeout(fnCallback, WordPause);
                }
                }

                // start a typewriter animation in the dataText array
                // @param int j = dataText array word index
                function StartTextAnimation(j) {
                //console.log(j);
                //console.log(dataText.length);
                // restart animation after N seconds
                if (typeof dataText[j] == 'undefined' || j == dataText.length) {
                setTimeout(function() {
                StartTextAnimation(WordOffset);
                }, RestartInterval);
                }

                // check if dataText[j] exists
                else if (j < dataText[j].length) {
                // text exists! start typewriter animation
                typeWriter(dataText[j], 0, function() {
                // after callback (and whole text has been animated), start next word
                StartTextAnimation((j+1));
                });
                }
                }

                document.addEventListener('DOMContentLoaded', function(event) {
                // start text animation
                StartTextAnimation(WordOffset);
                });

                <div id="typewriter"></div>








                share|improve this answer













                You already have the condition to check for undefined index, but you missed the additional condition to restart your sequence.



                Also, you have some poor choice of variable names, namely, index "i" in both functions can be confusing, as one function iterates over words in array, and the other one over characters.



                I've rewritten your functions and make the entire code more production-friendly and human-readable of what-is-going-on:






                // array with text to type in typewriter
                var dataText = [
                "Web Design.",
                "Web Development.",
                "Web Programming."
                ];

                // typewriter speed
                // set delay time between each character typing time
                var CharDelay = 50;

                // pause time between each completed word (delay before next word starts)
                var WordPause = 1000;

                // set initial word in dataText array
                var WordOffset = 0;

                // set sequence restart interval N [ms]
                var RestartInterval = 3000;

                // type one text in the typewriter
                // keeps calling itself until complete word is printed
                function typeWriter(text, i, fnCallback) {
                // check if word isn't finished yet
                if (i < (text.length)) {
                // add next character to html
                document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

                // wait for a while and call this function again for next character
                setTimeout(function() {
                typeWriter(text, i+1, fnCallback)
                }, CharDelay);
                }

                // text finished, call callback if there is a callback function
                else if (typeof fnCallback == 'function') {
                // call callback after timeout
                setTimeout(fnCallback, WordPause);
                }
                }

                // start a typewriter animation in the dataText array
                // @param int j = dataText array word index
                function StartTextAnimation(j) {
                //console.log(j);
                //console.log(dataText.length);
                // restart animation after N seconds
                if (typeof dataText[j] == 'undefined' || j == dataText.length) {
                setTimeout(function() {
                StartTextAnimation(WordOffset);
                }, RestartInterval);
                }

                // check if dataText[j] exists
                else if (j < dataText[j].length) {
                // text exists! start typewriter animation
                typeWriter(dataText[j], 0, function() {
                // after callback (and whole text has been animated), start next word
                StartTextAnimation((j+1));
                });
                }
                }

                document.addEventListener('DOMContentLoaded', function(event) {
                // start text animation
                StartTextAnimation(WordOffset);
                });

                <div id="typewriter"></div>








                // array with text to type in typewriter
                var dataText = [
                "Web Design.",
                "Web Development.",
                "Web Programming."
                ];

                // typewriter speed
                // set delay time between each character typing time
                var CharDelay = 50;

                // pause time between each completed word (delay before next word starts)
                var WordPause = 1000;

                // set initial word in dataText array
                var WordOffset = 0;

                // set sequence restart interval N [ms]
                var RestartInterval = 3000;

                // type one text in the typewriter
                // keeps calling itself until complete word is printed
                function typeWriter(text, i, fnCallback) {
                // check if word isn't finished yet
                if (i < (text.length)) {
                // add next character to html
                document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

                // wait for a while and call this function again for next character
                setTimeout(function() {
                typeWriter(text, i+1, fnCallback)
                }, CharDelay);
                }

                // text finished, call callback if there is a callback function
                else if (typeof fnCallback == 'function') {
                // call callback after timeout
                setTimeout(fnCallback, WordPause);
                }
                }

                // start a typewriter animation in the dataText array
                // @param int j = dataText array word index
                function StartTextAnimation(j) {
                //console.log(j);
                //console.log(dataText.length);
                // restart animation after N seconds
                if (typeof dataText[j] == 'undefined' || j == dataText.length) {
                setTimeout(function() {
                StartTextAnimation(WordOffset);
                }, RestartInterval);
                }

                // check if dataText[j] exists
                else if (j < dataText[j].length) {
                // text exists! start typewriter animation
                typeWriter(dataText[j], 0, function() {
                // after callback (and whole text has been animated), start next word
                StartTextAnimation((j+1));
                });
                }
                }

                document.addEventListener('DOMContentLoaded', function(event) {
                // start text animation
                StartTextAnimation(WordOffset);
                });

                <div id="typewriter"></div>





                // array with text to type in typewriter
                var dataText = [
                "Web Design.",
                "Web Development.",
                "Web Programming."
                ];

                // typewriter speed
                // set delay time between each character typing time
                var CharDelay = 50;

                // pause time between each completed word (delay before next word starts)
                var WordPause = 1000;

                // set initial word in dataText array
                var WordOffset = 0;

                // set sequence restart interval N [ms]
                var RestartInterval = 3000;

                // type one text in the typewriter
                // keeps calling itself until complete word is printed
                function typeWriter(text, i, fnCallback) {
                // check if word isn't finished yet
                if (i < (text.length)) {
                // add next character to html
                document.querySelector("#typewriter").innerHTML = text.substring(0, i+1) + '<span aria-hidden="true"></span>';

                // wait for a while and call this function again for next character
                setTimeout(function() {
                typeWriter(text, i+1, fnCallback)
                }, CharDelay);
                }

                // text finished, call callback if there is a callback function
                else if (typeof fnCallback == 'function') {
                // call callback after timeout
                setTimeout(fnCallback, WordPause);
                }
                }

                // start a typewriter animation in the dataText array
                // @param int j = dataText array word index
                function StartTextAnimation(j) {
                //console.log(j);
                //console.log(dataText.length);
                // restart animation after N seconds
                if (typeof dataText[j] == 'undefined' || j == dataText.length) {
                setTimeout(function() {
                StartTextAnimation(WordOffset);
                }, RestartInterval);
                }

                // check if dataText[j] exists
                else if (j < dataText[j].length) {
                // text exists! start typewriter animation
                typeWriter(dataText[j], 0, function() {
                // after callback (and whole text has been animated), start next word
                StartTextAnimation((j+1));
                });
                }
                }

                document.addEventListener('DOMContentLoaded', function(event) {
                // start text animation
                StartTextAnimation(WordOffset);
                });

                <div id="typewriter"></div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 2 at 5:31









                dev101dev101

                75611123




                75611123






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001139%2fhow-to-repeat-type-writer-effect-using-js%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas