0

This loops forever, how do I stop it at the marked location?

$.fn.writeText = function(content) { var contentArray = content.split(""), current = 0, elem = this; setInterval(function() { if(current < contentArray.length) { elem.text(elem.text() + contentArray[current++]); } else{ //stop here. } }, 100); }; $("#ID").writeText("Texty text text"); 

Thanks. I think it's supposed to be clearTimeout(), but I'm not sure how.

1

1 Answer 1

1

Use clearTimeout() as shown :-

$.fn.writeText = function(content) { var timer; var contentArray = content.split(""), current = 0, elem = this; timer = setInterval(function() { if(current < contentArray.length) { elem.text(elem.text() + contentArray[current++]); } else{ clearTimeout(timer); } }, 100); }; $("#ID").writeText("Texty text text"); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.