Advertisement
Niko454

Untitled

Apr 21st, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. (function($) {
  2. $.fn.countTo = function(options) {
  3. // merge the default plugin settings with the custom options
  4. options = $.extend({}, $.fn.countTo.defaults, options || {});
  5.  
  6. // how many times to update the value, and how much to increment the value on each update
  7. var loops = Math.ceil(options.speed / options.refreshInterval),
  8. increment = (options.to - options.from) / loops;
  9.  
  10. return $(this).each(function() {
  11. var _this = this,
  12. loopCount = 0,
  13. value = options.from,
  14. interval = setInterval(updateTimer, options.refreshInterval);
  15.  
  16. function updateTimer() {
  17. value += increment;
  18. loopCount++;
  19. $(_this).html(value.toFixed(options.decimals));
  20.  
  21. if (typeof(options.onUpdate) == 'function') {
  22. options.onUpdate.call(_this, value);
  23. }
  24.  
  25. if (loopCount >= loops) {
  26. clearInterval(interval);
  27. value = options.to;
  28.  
  29. if (typeof(options.onComplete) == 'function') {
  30. options.onComplete.call(_this, value);
  31. }
  32. }
  33. }
  34. });
  35. };
  36.  
  37. $.fn.countTo.defaults = {
  38. from: 0, // the number the element should start at
  39. to: 100, // the number the element should end at
  40. speed: 1000, // how long it should take to count between the target numbers
  41. refreshInterval: 100, // how often the element should be updated
  42. decimals: 0, // the number of decimal places to show
  43. onUpdate: null, // callback method for every time the element is updated,
  44. onComplete: null, // callback method for when the element finishes updating
  45. };
  46. })(jQuery);
  47.  
  48. jQuery(function($) {
  49. $('.timer').countTo({
  50. from: 1,
  51. to: 10000000000000000000,
  52. speed: 10000000000000000000000,
  53. refreshInterval: 100,
  54. onComplete: function(value) {
  55. console.debug(this);
  56. }
  57. });
  58. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement