# Problem with typewriter prototype

**URL:** <https://forum.kirupa.com/t/problem-with-typewriter-prototype/261612>\
**Category:** flash\
**Created:** [May 29, 2008, 9:32am UTC](https://forum.kirupa.com/t/problem-with-typewriter-prototype/261612 "2008-05-29T09:32:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theMonitor](https://avatars.discourse-cdn.com/v4/letter/t/ecb155/32.png) [@theMonitor](https://forum.kirupa.com/u/theMonitor)\
**Post date:** [May 29, 2008, 9:32am UTC](https://forum.kirupa.com/t/problem-with-typewriter-prototype/261612/1 "2008-05-29T09:32:01Z")

</div>

I wonder if anyone could help find a solution to a problem I’m having with a typewriter effect prototype?

The code I’m using to achieve the effect is:

```auto

TextField.prototype.typeWriter = function(type_str:String, type_speed:Number) {
	var type_field:TextField = this;
	type_field.text = "";
	var type_counter:Number = 0;
	var type_interval:Number = setInterval(function () {
		type_field.text = type_field.text+type_str.charAt(type_counter)
		type_counter++;
		if (type_counter == type_str.length) {
			clearInterval(type_interval);
		}
	}, type_speed)
}

```

Which is activated by

```auto

myTextField.typeWriter(myText, typeSpeed);

```

It all works fine, but my problem is that if I use it to display a new string in the same text field before the previous string has finished ‘typing’, the previous string and new string are typed at the same time, resulting in a spliced textfield of gobbledegook - characters from each string are displayed alternately, for example:

If I feed “goodbye” into the function before “hello” has finished, I get “hgeololdobye”.

I presume that what I need to do is clear the previous string before using a new string, which I’ve tried to do by using

```auto

myTextField.text = "";

```

before calling the typewriter function, but that doesn’t work. Can anyone tell me what I need to do? Something within the prototype? It’s reached tearing hair out stage…

Thanks

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 29, 2008, 5:13pm UTC](https://forum.kirupa.com/t/problem-with-typewriter-prototype/261612/2 "2008-05-29T17:13:52Z")

</div>

What you actually need to do is to check to see whether type\_interval exists. If it does, then you should clear the interval like so:

```auto
TextField.prototype.typeWriter = function(type_str:String, type_speed:Number) {
	if (type_interval != null) {
		trace("clearing previous Interval");
		clearInterval(type_interval);
	}
	var type_field:TextField = this;
	var type_counter:Number = 0;
	type_interval = setInterval(function () {
		type_field.text = type_field.text + type_str.charAt(type_counter);
		type_counter++;
		if (type_counter == type_str.length) {
			clearInterval(type_interval);
		}
	}, type_speed);
};
myTextField.typeWriter("hello", 200);
myTextField.typeWriter("goodbye", 200);
myTextField.typeWriter("problem solved", 200);

```

---

<div class="post-metadata">

**Author:** ![glosrfc](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/glosrfc/32/8334_2.png) [@glosrfc](https://forum.kirupa.com/u/glosrfc)\
**Post date:** [May 29, 2008, 5:41pm UTC](https://forum.kirupa.com/t/problem-with-typewriter-prototype/261612/3 "2008-05-29T17:41:43Z")

</div>

Forgot to mention…the other change was to \*\*not \*\*use the var statement when declaring your interval. Using var causes the interval to be declared locally so that it only exists inside the setInterval function; therefore any attempt to check whether it exists (because you’re already running an instance of the typeWriter function), or to delete it outside of the setInterval function, will fail.
