Skip to content
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

Suggestion to handle secondsElapsed within this #4

Closed
PatriceBX opened this issue Dec 12, 2014 · 3 comments
Closed

Suggestion to handle secondsElapsed within this #4

PatriceBX opened this issue Dec 12, 2014 · 3 comments

Comments

@PatriceBX
Copy link

Hello John, and thanks for sharing.
After calling countdown.stop(), it is not possible to retrieve the value of the stopped countdown since secondsElapsed is not stored, but always calculated as per current time. I would suggest to handle secondsElapsed within "this". Then update this.secondsElapsed in _draw only, and all other functions in countdown would just take this.secondsElapsed. This would allow to add a function such as:

getSecondsElapsed: function () {
  return this.secondsElapsed;
},

What do you think ?

@johnschult
Copy link
Owner

Makes good sense. Can you submit a Pull Request with your proposed changes for review?

@PatriceBX
Copy link
Author

It seems submitting a pull request requires me creating a branch first, but I could not figure out how to create a branch, or even if I'm allowed to.
Here is a patch of my changes:

diff --git a/app/static/js/jquery.countdown360.js b/app/static/js/jquery.countdown360.js
index 301e597..e5b6f5e 100644
--- a/app/static/js/jquery.countdown360.js
+++ b/app/static/js/jquery.countdown360.js
@@ -36,16 +44,19 @@
       }
     },

-    addSeconds: function (value) {
-      var secondsElapsed = Math.round((new Date().getTime() - this.startedAt.getTime())/1000);
+    addSeconds : function (value) {
       if (this.settings.startOverAfterAdding) {
-          this.settings.seconds = this._secondsLeft(secondsElapsed) + parseInt(value);
+          this.settings.seconds = this._secondsLeft(this.secondsElapsed) + parseInt(value);
           this.start();
         } else {
           this.settings.seconds += parseInt(value);
         }
     },

+    getSecondsElapsed: function () {
+      return this.secondsElapsed;
+    },
+
     start: function () {
       this.startedAt = new Date();
       this._drawCountdownShape(Math.PI*3.5, true);
@@ -64,6 +75,7 @@
       this.settings.arcX = this.settings.radius + this.settings.strokeWidth;
       this.settings.arcY = this.settings.arcX;
       this._initPen(this._getCanvas());
+      this.secondsElapsed = 0;
       if (this.settings.autostart) { this.start(); }
     },

@@ -91,17 +103,17 @@
       this.pen.clearRect(0, 0, this.settings.width, this.settings.height);
     },

-    _secondsLeft: function(secondsElapsed) {
-      return this.settings.seconds - secondsElapsed;
+    _secondsLeft: function() {
+      return this.settings.seconds - this.secondsElapsed;
     },

-    _drawCountdownLabel: function (secondsElapsed) {
-      this.ariaText.text(secondsLeft);
+    _drawCountdownLabel: function () {
       this.pen.font         = this.settings.fontWeight + " " + this.settings.fontSize + "px " + this.settings.fontFamily;
-      var secondsLeft = this._secondsLeft(secondsElapsed),
+      var secondsLeft = this._secondsLeft(),
           label = secondsLeft === 1 ? this.settings.label[0] : this.settings.label[1],
           drawLabel = this.settings.label && this.settings.label.length === 2,
           x = this.settings.width/2;
+      this.ariaText.text(secondsLeft);
       if (drawLabel) {
         y = this.settings.height/2 - (this.settings.fontSize/6.2);
       } else {
@@ -126,14 +138,15 @@
     },

     _draw: function () {
-      var secondsElapsed = Math.round((new Date().getTime() - this.startedAt.getTime())/1000),
-          endAngle = (Math.PI*3.5) - (((Math.PI*2)/this.settings.seconds) * secondsElapsed);
+      this.secondsElapsed = Math.round((new Date().getTime() - this.startedAt.getTime())/1000),
+          endAngle = (Math.PI*3.5) - (((Math.PI*2)/this.settings.seconds) * this.secondsElapsed);
       this._clearRect();
       this._drawCountdownShape(Math.PI*3.5, false);
-      if (secondsElapsed < this.settings.seconds) {
+      if (this.secondsElapsed < this.settings.seconds) {
         this._drawCountdownShape(endAngle, true);
-        this._drawCountdownLabel(secondsElapsed);
+        this._drawCountdownLabel();
       } else {
+        this.secondsElapsed = this.settings.seconds;
         this._drawCountdownLabel(this.settings.seconds);
         this.stop();
         this.settings.onComplete();

@johnschult
Copy link
Owner

Closing as this is addressed in #8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants