Skip to content

Commit 4ef9428

Browse files
committed
stop and start methods and callbacks
1 parent b20a815 commit 4ef9428

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

src/lib/MonoTyped.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ export default class Typed {
44
this.speed = config.typeSpeed; // use default type speed
55
this.nextPause = null;
66
this.config = config;
7+
this.timeout = null;
78

89
this.el = typeof element === 'string'
910
? document.querySelector(element)
1011
: element;
1112

1213
this.nodeCounter = 0;
1314
this.setDisplay(this.el, config.strings[0]);
14-
const spans = this.el.querySelectorAll('span');
15+
this.spans = this.el.querySelectorAll('span');
1516

1617
// We only have one string per instance, so these callbacks are equivalent.
1718
this.config.onBegin(this);
1819
this.config.preStringTyped(0, this);
1920

20-
this.typewrite(spans);
21+
this.typewrite();
2122
}
2223

2324
get strings () {
@@ -43,7 +44,17 @@ export default class Typed {
4344
element.replaceChildren(...newElement.childNodes);
4445
}
4546

47+
stop () {
48+
clearTimeout(this.timeout);
49+
this.config.onStop(0, this);
50+
}
4651

52+
start () {
53+
if(!this.timeout) {
54+
this.typewrite();
55+
this.config.onStart(0, this);
56+
}
57+
}
4758

4859
parseString (curString) {
4960

@@ -102,7 +113,7 @@ export default class Typed {
102113

103114
}
104115

105-
typewrite (characters) {
116+
typewrite () {
106117
if (this.actions[this.nodeCounter]) {
107118
this.executeAction(this.actions[this.nodeCounter]);
108119
}
@@ -112,18 +123,20 @@ export default class Typed {
112123
this.nextPause = null;
113124
this.config.onTypingResumed(0, this);
114125
}
115-
characters[this.nodeCounter].style = '';
126+
this.spans[this.nodeCounter].style = '';
116127
this.nodeCounter += 1;
117-
if (this.nodeCounter < characters.length) {
118-
this.typewrite(characters);
128+
if (this.nodeCounter < this.spans.length) {
129+
this.typewrite();
119130
} else {
131+
this.timeout = null;
120132
this.config.onStringTyped(0, this);
121133
}
122134
}, waitTime);
123135
}
124136

125137
destroy () {
126138
clearTimeout(this.timeout);
139+
this.timeout = null;
127140
this.el.replaceChildren();
128141
this.config.onDestroy(this);
129142
}

0 commit comments

Comments
 (0)