From a553157372f8e71c0973266797b991abd928ee26 Mon Sep 17 00:00:00 2001 From: Dmitriy Nikifrov Date: Sun, 12 Jan 2020 00:57:15 +0500 Subject: [PATCH] add "count" prop to end infnity typing --- src/ReactRotatingText.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ReactRotatingText.js b/src/ReactRotatingText.js index 41518b0..87c6bdc 100644 --- a/src/ReactRotatingText.js +++ b/src/ReactRotatingText.js @@ -11,6 +11,7 @@ class ReactRotatingText extends React.Component { index: random ? Math.floor(Math.random() * Math.floor(items.length)) : 0, output: '', substrLength: 0, + count: 0 }; this.timeouts = []; } @@ -24,6 +25,7 @@ class ReactRotatingText extends React.Component { } _loop(loopingFunc, pause) { + if(this.state.count == this.props.count) return; // save the timeouts so we can stop on unmount const timeout = setTimeout(loopingFunc, pause); this.timeouts.push(timeout); @@ -48,9 +50,21 @@ class ReactRotatingText extends React.Component { // if we're still not done, recursively loop again if (output.length < word.length) { this._loop(loopingFunc, typingInterval); - } else { + } + else { + this.setState({ + count: this.state.count + 1 + }); + if(this.props.count){ + if(this.props.count == this.state.count){ + this.timeouts.map(x => clearTimeout(x)); + this.timeouts = []; + console.log("done"); + } + } if (typeof this.props.onTypingEnd == 'function') { - this.props.onTypingEnd(); + this.props.onTypingEnd(); + } callback(); } @@ -150,7 +164,7 @@ class ReactRotatingText extends React.Component { return ( + aria-label={ this.props.items[this.state.index] }>> { this.state.output } { cursor ? | : null }