Advertisement
zaksya

Untitled

Feb 22nd, 2018
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TypingText = function(element, interval, cursor, finishedCallback) {
  2.   if((typeof document.getElementById == "undefined") || (typeof
  3.  
  4. element.innerHTML == "undefined")) {
  5.     this.running = true;
  6.     return;
  7.   }
  8.   this.element = element;
  9.   this.finishedCallback = (finishedCallback ? finishedCallback : function() {
  10.  
  11. return; });
  12.   this.interval = (typeof interval == "undefined" ? 100 : interval);
  13.   this.origText = this.element.innerHTML;
  14.   this.unparsedOrigText = this.origText;
  15.   this.cursor = (cursor ? cursor : "");
  16.   this.currentText = "";
  17.   this.currentChar = 0;
  18.   this.element.typingText = this;
  19.   if(this.element.id == "") this.element.id = "typingtext" +
  20.  
  21. TypingText.currentIndex++;
  22.   TypingText.all.push(this);
  23.   this.running = false;
  24.   this.inTag = false;
  25.   this.tagBuffer = "";
  26.   this.inHTMLEntity = false;
  27.   this.HTMLEntityBuffer = "";
  28. }
  29. TypingText.all = new Array();
  30. TypingText.currentIndex = 0;
  31. TypingText.runAll = function() {
  32.   for(var i = 0; i < TypingText.all.length; i++) TypingText.all[i].run();
  33. }
  34. TypingText.prototype.run = function() {
  35.   if(this.running) return;
  36.   if(typeof this.origText == "undefined") {
  37.     setTimeout("document.getElementById('" + this.element.id + "').typingText.run()", this.interval);
  38.     return;
  39.   }
  40.   if(this.currentText == "") this.element.innerHTML = "";
  41.   if(this.currentChar < this.origText.length) {
  42.     if(this.origText.charAt(this.currentChar) == "<" && !this.inTag) {
  43.       this.tagBuffer = "<";
  44.       this.inTag = true;
  45.       this.currentChar++;
  46.       this.run();
  47.       return;
  48.     } else if(this.origText.charAt(this.currentChar) == ">" && this.inTag) {
  49.       this.tagBuffer += ">";
  50.       this.inTag = false;
  51.       this.currentText += this.tagBuffer;
  52.       this.currentChar++;
  53.       this.run();
  54.       return;
  55.     } else if(this.inTag) {
  56.       this.tagBuffer += this.origText.charAt(this.currentChar);
  57.       this.currentChar++;
  58.       this.run();
  59.       return;
  60.     } else if(this.origText.charAt(this.currentChar) == "&" && !
  61.  
  62. this.inHTMLEntity) {
  63.       this.HTMLEntityBuffer = "&";
  64.       this.inHTMLEntity = true;
  65.       this.currentChar++;
  66.       this.run();
  67.       return;
  68.     } else if(this.origText.charAt(this.currentChar) == ";" &&
  69.  
  70. this.inHTMLEntity) {
  71.       this.HTMLEntityBuffer += ";";
  72.       this.inHTMLEntity = false;
  73.       this.currentText += this.HTMLEntityBuffer;
  74.       this.currentChar++;
  75.       this.run();
  76.       return;
  77.     } else if(this.inHTMLEntity) {
  78.       this.HTMLEntityBuffer += this.origText.charAt(this.currentChar);
  79.       this.currentChar++;
  80.       this.run();
  81.       return;
  82.     } else {
  83.       this.currentText += this.origText.charAt(this.currentChar);
  84.     }
  85.     this.element.innerHTML = this.currentText;
  86.     this.element.innerHTML += (this.currentChar < this.origText.length - 1 ? (typeof this.cursor == "function" ? this.cursor(this.currentText) : this.cursor) : "");
  87.     this.currentChar++;
  88.     setTimeout("document.getElementById('" + this.element.id + "').typingText.run()", this.interval);
  89.   } else {
  90.     this.currentText = "";
  91.     this.currentChar = 0;
  92.         this.running = false;
  93.         this.finishedCallback();
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement