Teletype Text in JavaScript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function teletype(id, text)
  2. {
  3. function typechar( char )
  4. {
  5. if ( char == 'â—„' && document.getElementById(id).innerHTML >= 1)
  6. {
  7. document.getElementById(id).innerHTML = document.getElementById(id).innerHTML.substr(0, document.getElementById(id).innerHTML.length-1);
  8. }
  9. else
  10. {
  11. document.getElementById(id).innerHTML += char;
  12. }
  13. }
  14.  
  15. var counter = 0;
  16. for( var i=0; i<text.length; i++ )
  17. {
  18. setTimeout(function()
  19. {
  20. var char = text[counter];
  21. typechar(char);
  22. counter++;
  23. },
  24. 50 * i);
  25. }
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.