Skip to content

Commit c12680a

Browse files
authored
Add onCompleteCallback option (#303)
* add onCompleteCallback option * update demo * new tests * update docs * update index
1 parent dae5a25 commit c12680a

13 files changed

+3276
-2705
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ interface CountUpOptions {
6666
enableScrollSpy?: boolean; // start animation when target is in view
6767
scrollSpyDelay?: number; // delay (ms) after target comes into view
6868
scrollSpyOnce?: boolean; // run only once
69+
onCompleteCallback?: () => any; // gets called when animation completes
6970
}
7071
```
7172

@@ -88,9 +89,12 @@ const countUp = new CountUp('targetId', 5234, options);
8889
with optional callback:
8990

9091
```js
91-
countUp.start(someMethodToCallOnComplete);
92+
const countUp = new CountUp('targetId', 5234, { onCompleteCallback: someMethod });
9293

93-
// or an anonymous function
94+
// or (passing fn to start will override options.onCompleteCallback)
95+
countUp.start(someMethod);
96+
97+
// or
9498
countUp.start(() => console.log('Complete!'));
9599
```
96100

demo/demo-nomodule.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,7 @@ window.onload = function () {
105105
demo = new countUp.CountUp('myTargetElement', endVal, options);
106106
if (!demo.error) {
107107
errorSection.style.display = 'none';
108-
if (el('useOnComplete').checked) {
109-
demo.start(methodToCallOnComplete);
110-
}
111-
else {
112-
demo.start();
113-
}
108+
demo.start();
114109
updateCodeVisualizer();
115110
}
116111
else {
@@ -137,7 +132,8 @@ window.onload = function () {
137132
decimal: el('decimal').value,
138133
prefix: el('prefix').value,
139134
suffix: el('suffix').value,
140-
numerals: getNumerals()
135+
numerals: getNumerals(),
136+
onCompleteCallback: el('useOnComplete').checked ? methodToCallOnComplete : null
141137
};
142138
// unset null values so they don't overwrite defaults
143139
for (var key in options) {
@@ -179,6 +175,8 @@ window.onload = function () {
179175
opts += (options.suffix.length) ? indentedLine("suffix: '" + options.suffix + "'") : '';
180176
opts += (options.numerals && options.numerals.length) ?
181177
indentedLine("numerals: " + stringifyArray(options.numerals)) : '';
178+
opts += (options.onCompleteCallback) ? indentedLine("onCompleteCallback: methodToCallOnComplete") : '';
179+
182180
if (opts.length) {
183181
code += "const options = {<br>" + opts + "};<br>";
184182
code += "let demo = new CountUp('myTargetElement', " + endVal + ", options);<br>";
@@ -187,8 +185,7 @@ window.onload = function () {
187185
code += "let demo = new CountUp('myTargetElement', " + endVal + ");<br>";
188186
}
189187
code += 'if (!demo.error) {<br>';
190-
code += (el('useOnComplete').checked) ?
191-
indentedLine('demo.start(methodToCallOnComplete)', true) : indentedLine('demo.start()', true);
188+
code += indentedLine('demo.start()', true);
192189
code += '} else {<br>';
193190
code += indentedLine('console.error(demo.error)', true);
194191
code += '}';

demo/demo.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,7 @@ window.onload = function () {
103103
if (!demo.error) {
104104
errorSection.style.display = 'none';
105105
startTime = Date.now();
106-
if (el('useOnComplete').checked) {
107-
demo.start(methodToCallOnComplete);
108-
}
109-
else {
110-
demo.start(() => calculateAnimationTime());
111-
}
106+
demo.start();
112107
updateCodeVisualizer();
113108
}
114109
else {
@@ -120,10 +115,6 @@ window.onload = function () {
120115
function calculateAnimationTime() {
121116
const duration = Date.now() - startTime;
122117
console.log('actual animation duration (ms):', duration);
123-
}
124-
function methodToCallOnComplete() {
125-
calculateAnimationTime();
126-
console.log('COMPLETE!');
127118
alert('COMPLETE!');
128119
}
129120
function establishOptionsFromInputs() {
@@ -140,7 +131,8 @@ window.onload = function () {
140131
decimal: el('decimal').value,
141132
prefix: el('prefix').value,
142133
suffix: el('suffix').value,
143-
numerals: getNumerals()
134+
numerals: getNumerals(),
135+
onCompleteCallback: el('useOnComplete').checked ? calculateAnimationTime : null
144136
};
145137
// unset null values so they don't overwrite defaults
146138
for (var key in options) {
@@ -182,6 +174,8 @@ window.onload = function () {
182174
opts += (options.suffix.length) ? indentedLine("suffix: '" + options.suffix + "'") : '';
183175
opts += (options.numerals && options.numerals.length) ?
184176
indentedLine("numerals: " + stringifyArray(options.numerals)) : '';
177+
opts += (options.onCompleteCallback) ? indentedLine("onCompleteCallback: methodToCallOnComplete") : '';
178+
185179
if (opts.length) {
186180
code += "const options = {<br>" + opts + "};<br>";
187181
code += "let demo = new CountUp('myTargetElement', " + endVal + ", options);<br>";
@@ -190,8 +184,7 @@ window.onload = function () {
190184
code += "let demo = new CountUp('myTargetElement', " + endVal + ");<br>";
191185
}
192186
code += 'if (!demo.error) {<br>';
193-
code += (el('useOnComplete').checked) ?
194-
indentedLine('demo.start(methodToCallOnComplete)', true) : indentedLine('demo.start()', true);
187+
code += indentedLine('demo.start()', true);
195188
code += '} else {<br>';
196189
code += indentedLine('console.error(demo.error)', true);
197190
code += '}';

dist/countUp.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface CountUpOptions {
1717
enableScrollSpy?: boolean;
1818
scrollSpyDelay?: number;
1919
scrollSpyOnce?: boolean;
20+
onCompleteCallback?: () => any;
2021
}
2122
export declare class CountUp {
2223
private endVal;
@@ -32,7 +33,6 @@ export declare class CountUp {
3233
private countDown;
3334
formattingFn: (num: number) => string;
3435
easingFn?: (t: number, b: number, c: number, d: number) => number;
35-
callback: (args?: any) => any;
3636
error: string;
3737
startVal: number;
3838
duration: number;

dist/countUp.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var CountUp = /** @class */ (function () {
1515
var _this = this;
1616
this.endVal = endVal;
1717
this.options = options;
18-
this.version = '2.4.2';
18+
this.version = '2.5.0';
1919
this.defaults = {
2020
startVal: 0,
2121
decimalPlaces: 0,
@@ -74,8 +74,8 @@ var CountUp = /** @class */ (function () {
7474
_this.update(_this.finalEndVal);
7575
}
7676
else {
77-
if (_this.callback) {
78-
_this.callback();
77+
if (_this.options.onCompleteCallback) {
78+
_this.options.onCompleteCallback();
7979
}
8080
}
8181
};
@@ -206,7 +206,9 @@ var CountUp = /** @class */ (function () {
206206
if (this.error) {
207207
return;
208208
}
209-
this.callback = callback;
209+
if (callback) {
210+
this.options.onCompleteCallback = callback;
211+
}
210212
if (this.duration > 0) {
211213
this.determineDirectionAndSmartEasing();
212214
this.paused = false;

dist/countUp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/countUp.umd.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
var _this = this;
2222
this.endVal = endVal;
2323
this.options = options;
24-
this.version = '2.4.2';
24+
this.version = '2.5.0';
2525
this.defaults = {
2626
startVal: 0,
2727
decimalPlaces: 0,
@@ -80,8 +80,8 @@
8080
_this.update(_this.finalEndVal);
8181
}
8282
else {
83-
if (_this.callback) {
84-
_this.callback();
83+
if (_this.options.onCompleteCallback) {
84+
_this.options.onCompleteCallback();
8585
}
8686
}
8787
};
@@ -212,7 +212,9 @@
212212
if (this.error) {
213213
return;
214214
}
215-
this.callback = callback;
215+
if (callback) {
216+
this.options.onCompleteCallback = callback;
217+
}
216218
if (this.duration > 0) {
217219
this.determineDirectionAndSmartEasing();
218220
this.paused = false;

0 commit comments

Comments
 (0)