Skip to content
Browse files

add Spinner#stop()

  • Loading branch information...
1 parent cd5e2a8 commit 683c5924b75957b11c1e83007b3287c9f73e3fb4 @tj tj committed Feb 14, 2013
Showing with 22 additions and 1 deletion.
  1. +6 −0 Readme.md
  2. +13 −1 index.js
  3. +3 −0 test/index.html
View
6 Readme.md
@@ -43,6 +43,12 @@ document.body.appendChild(spinner.el);
Change the speed to `n` rpm, defaults to 60.
+### Spinner#stop()
+
+ Stop the animation. This is important when removing
+ the spinner, otherwise the requestAnimationFrame cycle
+ will continue.
+
## License
MIT
View
14 index.js
@@ -17,23 +17,35 @@ module.exports = Spinner;
*/
function Spinner() {
+ var self = this;
this.percent = 0;
this.el = document.createElement('canvas');
this.ctx = this.el.getContext('2d');
this.size(50);
this.fontSize(11);
this.speed(60);
this.font('helvetica, arial, sans-serif');
+ this.stopped = false;
- var self = this;
(function animate() {
+ if (self.stopped) return;
raf(animate);
self.percent = (self.percent + self._speed / 36) % 100;
self.draw(self.ctx);
})();
}
/**
+ * Stop the animation.
+ *
+ * @api public
+ */
+
+Spinner.prototype.stop = function(){
+ this.stopped = true;
+};
+
+/**
* Set spinner size to `n`.
*
* @param {Number} n
View
3 test/index.html
@@ -57,6 +57,9 @@
var spinner = new Spinner;
spinner.size(25).speed(30).light();
document.querySelector('.dark').appendChild(spinner.el);
+ setTimeout(function(){
+ spinner.stop();
+ }, 2000);
})();
</script>

0 comments on commit 683c592

Please sign in to comment.
Something went wrong with that request. Please try again.