Skip to content

Instantly share code, notes, and snippets.

@amadeus
Created March 31, 2014 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amadeus/9901638 to your computer and use it in GitHub Desktop.
Save amadeus/9901638 to your computer and use it in GitHub Desktop.
(function(){
this.Terminal = function(container, steps){
this.container = document.getElementById(container);
this.steps = steps;
this.cycle = this.cycle.bind(this);
this.cycle();
};
this.Terminal.prototype.cycle = function(){
if (!this.current) {
if (!this.steps.length) {
return;
}
this.current = this.steps.shift();
if (this.current.display === 'typewriter') {
this.current.text = this.current.text.split('');
}
if (this.current.delay) {
clearTimeout(this._timer);
this._timer = setTimeout(this.cycle, this.current.delay);
return;
}
}
if (this.current.display === 'typewriter') {
if (!this.current.element) {
this.current.element = document.createElement('div');
this.container.appendChild(this.current.element);
this.current.element.innerHTML = this.current.prefix || '';
}
this.current.element.innerHTML += this.current.text.shift();
if (!this.current.text.length) {
this.current = null;
return this.cycle();
} else {
clearTimeout(this._timer);
this._timer = setTimeout(this.cycle, 40);
return;
}
}
if (this.current.display === 'instant') {
this.current.element = document.createElement('div');
this.current.element.innerHTML = this.current.text;
this.container.appendChild(this.current.element);
this.current = null;
return this.cycle();
}
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment