From 0d6b67a36173f243b6c3a033b659654b4d354e53 Mon Sep 17 00:00:00 2001 From: alaasdk Date: Mon, 22 Jun 2015 09:43:57 +0200 Subject: [PATCH 1/2] Change directive name in the help page. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 38ba862..03a478f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ 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. From 4be468b01e4fd5ee6075da6a1a83d674f7ac3a9c Mon Sep 17 00:00:00 2001 From: alaasdk Date: Mon, 22 Jun 2015 09:53:34 +0200 Subject: [PATCH 2/2] add support to "startup-delay" attribute, if you want to delay the counter start. --- README.md | 4 ++-- build/angular-count-to.min.js | 4 ++-- src/count-to.js | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 03a478f..7fdd57b 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ var myApp = angular.module('myApp', ['countTo']); Apply the directive to a dom element. ``` - + ``` @@ -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. \ No newline at end of file diff --git a/build/angular-count-to.min.js b/build/angular-count-to.min.js index 91e224e..74b993a 100644 --- a/build/angular-count-to.min.js +++ b/build/angular-count-to.min.js @@ -1,2 +1,2 @@ -/*! angular-count-to 2013-07-16 */ -var countTo=angular.module("countTo",[]).directive("countTo",["$timeout",function(a){return{replace:!1,scope:!0,link:function(b,c,d){var e,f,g,h,i,j,k,l=c[0],m=function(){f=30,i=0,b.timoutId=null,j=parseInt(d.countTo)||0,b.value=parseInt(d.value,10)||0,g=1e3*parseFloat(d.duration)||0,h=Math.ceil(g/f),k=(j-b.value)/h,e=b.value},n=function(){b.timoutId=a(function(){e+=k,i++,i>=h?(a.cancel(b.timoutId),e=j,l.innerText=j):(l.innerText=Math.round(e),n())},f)},o=function(){b.timoutId&&a.cancel(b.timoutId),m(),n()};return d.$observe("countTo",function(a){a&&o()}),d.$observe("value",function(){o()}),!0}}}]); \ No newline at end of file + +var countTo=angular.module("countTo",[]).directive("countTo",["$timeout",function(n){return{replace:!1,scope:!0,link:function(t,i,r){var e=i[0],u,o,c,s,h,f,l,a,p=function(){o=30;h=0;t.timoutId=null;t.delayTimeoutId=null;f=parseInt(r.countTo)||0;t.value=parseInt(r.value,10)||0;c=parseFloat(r.duration)*1e3||0;a=parseFloat(r.startupDelay)*1e3||0;s=Math.ceil(c/o);l=(f-t.value)/s;u=t.value},v=function(){t.timoutId=n(function(){u+=l;h++;h>=s?(n.cancel(t.timoutId),u=f,e.textContent=f):(e.textContent=Math.round(u),v())},o)},y=function(){t.delayTimeoutId&&n.cancel(t.delayTimeoutId);t.timoutId&&n.cancel(t.timoutId);p();e.textContent=t.value;t.delayTimeoutId=n(function(){v()},a)};return r.$observe("countTo",function(n){n&&y()}),r.$observe("value",function(){y()}),!0}}}]); \ No newline at end of file diff --git a/src/count-to.js b/src/count-to.js index d3aa7ad..9602731 100644 --- a/src/count-to.js +++ b/src/count-to.js @@ -1,3 +1,4 @@ + var countTo = angular.module('countTo', []) .directive('countTo', ['$timeout', function ($timeout) { return { @@ -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); @@ -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) {