Skip to content

add support to "startup-delay" attribute to enable delay the counter. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ Include the javascript file.
Inject the `count-to` directive in your app.

```
var myApp = angular.module('myApp', ['count-to']);
var myApp = angular.module('myApp', ['countTo']);
```

Apply the directive to a dom element.
```
<span count-to="{{countTo}}" value="{{countFrom}}" duration="4"></span>
<span count-to="{{countTo}}" value="{{countFrom}}" duration="4" startup-delay="5"></span>
```


Expand All @@ -31,4 +31,4 @@ The following attributes can be set as numbers on the directive element.
- ```count-to``` the number to count to.
- ```value``` the number to start counting from.
- ```duration``` how long the count should take in seconds.

- ```startup-delay``` how long the counter will wait until it starts.
4 changes: 2 additions & 2 deletions build/angular-count-to.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions src/count-to.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

var countTo = angular.module('countTo', [])
.directive('countTo', ['$timeout', function ($timeout) {
return {
Expand All @@ -6,15 +7,17 @@ var countTo = angular.module('countTo', [])
link: function (scope, element, attrs) {

var e = element[0];
var num, refreshInterval, duration, steps, step, countTo, value, increment;
var num, refreshInterval, duration, steps, step, countTo, value, increment, startupDelay;

var calculate = function () {
refreshInterval = 30;
step = 0;
scope.timoutId = null;
scope.delayTimeoutId = null;
countTo = parseInt(attrs.countTo) || 0;
scope.value = parseInt(attrs.value, 10) || 0;
duration = (parseFloat(attrs.duration) * 1000) || 0;
startupDelay = (parseFloat(attrs.startupDelay) * 1000) || 0;

steps = Math.ceil(duration / refreshInterval);
increment = ((countTo - scope.value) / steps);
Expand All @@ -38,11 +41,17 @@ var countTo = angular.module('countTo', [])
}

var start = function () {
if (scope.timoutId) {
$timeout.cancel(scope.timoutId);
}

if (scope.delayTimeoutId) { $timeout.cancel(scope.delayTimeoutId); }
if (scope.timoutId) { $timeout.cancel(scope.timoutId); }

calculate();
tick();

e.textContent = scope.value;

scope.delayTimeoutId = $timeout(function () {
tick();
}, startupDelay);
}

attrs.$observe('countTo', function (val) {
Expand Down