Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Created April 18, 2012 05:40
Show Gist options
  • Save s-hiroshi/2411317 to your computer and use it in GitHub Desktop.
Save s-hiroshi/2411317 to your computer and use it in GitHub Desktop.
JavaScript > lib > It displays one character at a time.
<html>
<body>
<p id="foo"></p>
</body>
</html>
/**
* typo.js
*
* It displays one character at a time.
*
* Copyright 2012 Sawai Hiroshi
* http://www.info-town.jp
*
*/
/**
* typographic object
*/
var typo = {};
// implement typo object
(function() {
/**
* transform string to array
*
* @private
* @param {String} text
* @return {Array} arr
*/
function _strToArr(str) {
var arr = [];
for (i = 0; i < str.length; i++) {
arr.push(str.charAt(i));
}
return arr;
}
/**
* insert text to dom
*
* callback of setTimeout
*
* @private
* @param {jQuery} elem
* @param {String} text
* @param {Number} timer_id Timer ID
*/
function _insert(text, elem) {
// argument elem, text is free variable
return function _callback() {
console.log(elem);
elem.innerHTML = text; // insert text
clearTimeout(_callback.timer);
};
}
/**
* rendering text used timer
*
* @private
* @param {Array} arr
* @param {jQuery} elem
*/
function _rendering(text, elem) {
var arr = _strToArr(text),
timer = [],
text,
s,
i;
for (i = 0; i < arr.length; i++) {
text = arr.slice(0, i + 1).join('');
s = _insert(text, elem);
s.timer = setTimeout(s, i * 500);
}
}
typo.rendering = _rendering;
}());
// var elem = document.getElementById('foo');
// typo.rendering('Hellow World', elem);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment