Ver Mensaje Individual
  #5 (permalink)  
Antiguo 03/02/2013, 13:48
Mndrake
 
Fecha de Ingreso: septiembre-2010
Mensajes: 78
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Cambiar mensaje al finalizar

Hola emprear esta es la funcion:

Código Javascript:
Ver original
  1. (function($) {
  2.  
  3.   $.fn.countdown = function(toDate, callback) {
  4.     var handlers = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'daysLeft'];
  5.    
  6.     function delegate(scope, method) {
  7.       return function() { return method.call(scope) }
  8.     }
  9.    
  10.     return this.each(function() {
  11.       // Convert
  12.       if(!(toDate instanceof Date)) {
  13.         if(String(toDate).match(/^[0-9]*$/)) {
  14.           toDate = new Date(toDate);
  15.         } else if( toDate.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/) ||
  16.             toDate.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})\s([0-9]{1,2})\:([0-9]{2})\:([0-9]{2})/)
  17.             ) {
  18.           toDate = new Date(toDate);
  19.         } else if(toDate.match(/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})/) ||
  20.                   toDate.match(/([0-9]{2,4})\/([0-9]{1,2})\/([0-9]{1,2})/)
  21.                   ) {
  22.           toDate = new Date(toDate)
  23.         } else {
  24.           throw new Error("Doesn't seen to be a valid date object or string")
  25.         }
  26.       }
  27.      
  28.       var $this = $(this),
  29.           values = {},
  30.           lasting = {},
  31.           interval = $this.data('countdownInterval'),
  32.           currentDate = new Date(),
  33.           secondsLeft = Math.floor((toDate.valueOf() - currentDate.valueOf()) / 1000);
  34.      
  35.       function triggerEvents() {
  36.         // Evaluate if this node is included in the html
  37.         if($this.closest('html').length === 0) {
  38.           stop(); // Release the memory
  39.           dispatchEvent('removed');
  40.           return;
  41.         }
  42.         // Calculate the time offset
  43.         secondsLeft--;
  44.         if(secondsLeft < 0) {
  45.           secondsLeft = 0;
  46.         }
  47.         lasting = {
  48.           seconds : secondsLeft % 60,
  49.           minutes : Math.floor(secondsLeft / 60) % 60,
  50.           hours   : Math.floor(secondsLeft / 60 / 60) % 24,
  51.           days    : Math.floor(secondsLeft / 60 / 60 / 24),
  52.           weeks   : Math.floor(secondsLeft / 60 / 60 / 24 / 7),
  53.           daysLeft: Math.floor(secondsLeft / 60 / 60 / 24) % 7
  54.         }
  55.         for(var i=0; i<handlers.length; i++) {
  56.           var eventName = handlers[i];
  57.           if(values[eventName] != lasting[eventName]) {
  58.             values[eventName] = lasting[eventName];
  59.             dispatchEvent(eventName);
  60.           }
  61.         }
  62.         if(secondsLeft == 0) {
  63.           stop();
  64.           dispatchEvent('finished');
  65.         }
  66.       }
  67.       triggerEvents();
  68.      
  69.       function dispatchEvent(eventName) {
  70.         var event     = $.Event(eventName);
  71.         event.date    = new Date(new Date().valueOf() + secondsLeft);
  72.         event.value   = values[eventName] || "0";
  73.         event.toDate  = toDate;
  74.         event.lasting = lasting;
  75.         switch(eventName) {
  76.           case "seconds":
  77.           case "minutes":
  78.           case "hours":
  79.             event.value = event.value < 10 ? '0'+event.value.toString() : event.value.toString();
  80.             break;
  81.           default:
  82.             if(event.value) {
  83.               event.value = event.value.toString();
  84.             }
  85.             break;
  86.         }
  87.         callback.call($this, event);
  88.       }
  89.      
  90.       function stop() {
  91.         clearInterval(interval);
  92.       }
  93.  
  94.       function start() {
  95.         $this.data('countdownInterval', setInterval(delegate($this, triggerEvents), 1000));
  96.         interval = $this.data('countdownInterval');
  97.       }
  98.      
  99.       if(interval) stop();
  100.       start();
  101.     });
  102.   }
  103. })(jQuery);