Basic Timer Question

Basic Timer Question


Hi all -

Bit of a newbie here, so apologies for the stupid q - hopefully this one is easily solved.

I'm trying to do add 4 countdown boxes to a website - I can find scripts for basic countdowns, but can't get it so that it has 4 on the one page as I'm really new to jquery. For example, I'd like to have 4 spans, (timer, timer2, timer 3 etc)

This is the code i have:

HTML:
  1. <span class="timer"></span>
  2. <hr/>
  3. <span id="help">From: 50 - To: 2500 / Over 5000 Milli-Seconds</span>

JS:

  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.         // how many times to update the value, and how much to increment the value on each update
  6.         var loops = Math.ceil(options.speed / options.refreshInterval),
  7.             increment = (options.to - options.from) / loops;

  8.         return $(this).each(function() {
  9.             var _this = this,
  10.                 loopCount = 0,
  11.                 value = options.from,
  12.                 interval = setInterval(updateTimer, options.refreshInterval);

  13.             function updateTimer() {
  14.                 value += increment;
  15.                 loopCount++;
  16.                 $(_this).html(value.toFixed(options.decimals));

  17.                 if (typeof(options.onUpdate) == 'function') {
  18.                     options.onUpdate.call(_this, value);
  19.                 }

  20.                 if (loopCount >= loops) {
  21.                     clearInterval(interval);
  22.                     value = options.to;

  23.                     if (typeof(options.onComplete) == 'function') {
  24.                         options.onComplete.call(_this, value);
  25.                     }
  26.                 }
  27.             }
  28.         });
  29.     };

  30.     $.fn.countTo.defaults = {
  31.         from: 0,  // the number the element should start at
  32.         to: 100,  // the number the element should end at
  33.         speed: 1000,  // how long it should take to count between the target numbers
  34.         refreshInterval: 100,  // how often the element should be updated
  35.         decimals: 0,  // the number of decimal places to show
  36.         onUpdate: null,  // callback method for every time the element is updated,
  37.         onComplete: null,  // callback method for when the element finishes updating
  38.     };
  39. })(jQuery);

  40. jQuery(function($) {
  41.         $('.timer').countTo({
  42.             from: 50,
  43.             to: 2500,
  44.             speed: 5000,
  45.             refreshInterval: 50,
  46.             onComplete: function(value) {
  47.                 console.debug(this);
  48.             }
  49.         });
  50.     });


Many thanks!