var el = document.getElementById('moc-1'); var source = '#monthofcode'; var target = 'hello world!'; var alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789#! ';
functionanimate(src, tgt, done){ functionstep(){ if (src === tgt) { return done(); } var nextSrc = ''; for (var i = 0; i < src.length; i++) { // increment each letter if target not reached if (src.charAt(i) !== tgt.charAt(i)) { var nextIndex = alphabet.indexOf(src.charAt(i)) + 1; nextIndex %= alphabet.length; nextSrc += alphabet.charAt(nextIndex); } else { nextSrc += src.charAt(i); } } el.innerHTML = nextSrc; src = nextSrc; setTimeout(step, 120); } step(); }
functionrun(){ animate(source, target, functiondone(){ // swap target and source for next iteration var tmp = source; source = target; target = tmp;