# Mathematica Typing Effect

Here is a Typing Effect in javascript.

I could only implement a very simple one:

DynamicModule[{s = ""},
Dynamic[Refresh[
If[Length[s] > 10, s = "", s = s <> "x"],
TrackedSymbols :> {}, UpdateInterval -> 0.5]]]


How can I do that exactly the same?

-

Perhaps

typingEffect[str_String, secPerLetter_: 0.3] :=
Dynamic[StringTake[str,
Clock[{0, StringLength@str, 1}, StringLength@str secPerLetter]]]


(thanks @SjoerdC.deVries)

So this gives an animation

typingEffect["hello my dear"]


Other effects can be implemented with the same idea, such as

typingEffect2[str_String] :=
Row@MapIndexed[
Style[#,
FontColor->Dynamic@ColorData["Rainbow"][
Mod[(First@#2 - 0.5)/StringLength@str + Clock[], 1]]] &,
Characters@str]

-
as would my 3 years old son say: "but why?" ;-) –  chris Nov 19 '12 at 17:48
@chris, I wouldn't know exactly what to explain to your 3 year old son :P. What part does he not understand? –  Rojo Nov 19 '12 at 17:50
well that's the thing: it doesn't matter what I explain to him... He always comes up with that question! –  chris Nov 19 '12 at 17:51
@chris, don't worry, he'll soon grow up like all of us and forget he never got his answer –  Rojo Nov 19 '12 at 17:52
@Rojo, typing StringLength@str/.slen_:> and then slen twice takes just as many characters as simply typing StringLength@str twice, but is perhaps more confusing. –  VF1 Nov 19 '12 at 18:22

Implementation with ScheduledTask. When the Type button is pushed, a scheduled task is started that increases the display length of the temporary string temp. It can be paused (Stop) and continued (Type again).

text = StringTake[ExampleData[{"Text", "LoremIpsum"}], 120]

temp = ""; task = None; n = 0;
temp = StringTake[text, n++]], 0.01]],
Dynamic@temp


-