Skip to content

Instantly share code, notes, and snippets.

@mrlubos
Created July 15, 2016 02:02
Show Gist options
  • Select an option

  • Save mrlubos/31a12ae1cf73a2d8c26d2568c34ccb8d to your computer and use it in GitHub Desktop.

Select an option

Save mrlubos/31a12ae1cf73a2d8c26d2568c34ccb8d to your computer and use it in GitHub Desktop.
// Returns a random number to simulate our thinking.
function getThinkingTime () {
var base = 500,
deviation = 250;
return (Math.random() * deviation * 2) + (base - deviation);
}
// Starts the animation.
function startAnimation () {
interval = setInterval(function () {
// Our copy of the line is empty.
if (line.length === 0) {
// Move the pointer to the next line.
pointer = pointer + 1;
// The pointer is inside the range, copy the new line.
if (pointer !== strings.length) {
line = strings[pointer];
// Thinking before typing, always a good idea!
pauseAnimation(getThinkingTime());
return;
}
// The pointer is out of range, stop the animation,
// reset the interval variable, and exit the loop.
else {
return interval = clearInterval(interval);
}
}
// Print the first character from our copy of the line.
app.print(line[0]);
// Now delete the first character, so the second character
// becomes the first, the third becomes second, etc.
line = line.substr(1);
// If we have reached the end of the line, fake the
// Enter key press action.
if (!line) {
pauseAnimation(getThinkingTime(), function () {
app.println();
});
}
}, 80);
}
// Stop and start the animation after a while,
// creating an illusion of the pause.
function pauseAnimation (time, callback) {
clearInterval(interval);
setTimeout(function () {
if (typeof callback === 'function') {
callback();
}
startAnimation();
}, time);
}
// Initialise the animation.
startAnimation();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment