Ver Mensaje Individual
  #5 (permalink)  
Antiguo 20/12/2012, 14:39
Mndrake
 
Fecha de Ingreso: septiembre-2010
Mensajes: 78
Antigüedad: 13 años, 7 meses
Puntos: 0
Respuesta: Usar multiples relojes sin duplicar el codigo...

Cita:
Iniciado por quimfv Ver Mensaje
Si has entendido bien.

Luego tienes que tener esos divs en el html.

La funcion debe recibir dos paramteros:

numClock el numero del div donde vas a mostrar el reloj

valorNumClock la fecha y/u hora que debe tener en cuenta

Por la sintaxis que empleas debes estar usando jquery o una libreria similar. No nos dices o no se ver como se desencadena el evento que ejecuta la funcion. Falta otra modificacion que es la definicion de los parametros...

El esquema en javascript sin librerias seria


Código Javascript:
Ver original
  1. function reloj(numClock,valorNumClock){
  2. ....
  3. }

y en algun evento en el html el llamado a la función con los con los datos

on....="reloj(1,'2012,12,24')" para el reloj 1 y esa fecha.

(No se porque usas comas y no 2012-12-24 ???)

Si los relojes no van ligados a un evento tambien podrias hacer lo siguiente

Código Javascript:
Ver original
  1. <script type="text/javascript">
  2. $(function(){
  3. var arrvalorNumClock=new Array();
  4. arrvalorNumClock[0]=<?php echo $clock0 ?>;
  5. arrvalorNumClock[1]=<?php echo $clock1 ?>;
  6. arrvalorNumClock[2]=<?php echo $clock2 ?>;
  7. ....
  8. arrvalorNumClock[14]=<?php echo $clock14 ?>;
  9. for (var numClock=0;numClock<15;numClock++){
  10.  var d, h, m, s;
  11.   $('div#clock'+numClock).countdown(new Date(arrvalorNumClock[numClock]), function(event) {
  12.     var timeFormat = "%d dia(s) %h:%m:%s"
  13.         $this = $(this);
  14.     switch(event.type) {
  15.       case "days":
  16.         d = event.value;
  17.         break;
  18.       case "hours":
  19.         h = event.value;
  20.         break;
  21.       case "minutes":
  22.         m = event.value;
  23.         break;
  24.       case "seconds":
  25.         s = event.value;
  26.         break;
  27.       case "finished":
  28.         $this.fadeTo('slow', 0.5);
  29.         break;
  30.     }
  31.     // Assemble time format
  32.     if(d > 0) {
  33.       timeFormat = timeFormat.replace(/\%d/, d);
  34.       timeFormat = timeFormat.replace(/\(s\)/, Number(d) == 1 ? '' : 's');
  35.     } else {
  36.       timeFormat = timeFormat.replace(/%d dia\(s\)/, '');
  37.     }
  38.     timeFormat = timeFormat.replace(/\%h/, h);
  39.     timeFormat = timeFormat.replace(/\%m/, m);
  40.     timeFormat = timeFormat.replace(/\%s/, s);
  41.     // Display
  42.     $this.html(timeFormat);
  43.  
  44.     //Cierro el bucle for
  45.  
  46.     }
  47.   });
  48. });
  49. </script>

Asi crearias directamente 15 relojes


Quizas estoy improvisando demasiado al no tener claro que libreria usas ni como ejecutas la función pero la idea es esa....

Hola quimfv gracias por tu ejemplo

las librerias estoy utilizando son :

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../src/jquery.countdown.js" type="text/javascript" charset="utf-8"></script>

el resultado que daria el ejemplo que pusiste es lo que necesito pero no me funciona

lo que quiero con un solo codigo tener tantos relojes como quiera

pero no me funciono al probarlo


lo que no quiero es tener que hacer esto:


<script type="text/javascript">
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){var d, h, m, s;$('div#reloj').countdown(new Date(<?php echo $reloj1 ?>),function(event){var timeFormat = "%d dia(s) %h:%m:%s"
$this = $(this);switch(event.type){case "days":d = event.value;break;case "hours":h=event.value;break;case "minutes":
m = event.value;break;case "seconds":s = event.value;break;case "finished":$this.fadeTo('slow', 0.5);break;}
if(d > 0){timeFormat = timeFormat.replace(/\%d/, d);timeFormat = timeFormat.replace(/\(s\)/, Number(d) == 1 ? '' : 's');
}else{timeFormat = timeFormat.replace(/%d dia\(s\)/, '');}timeFormat = timeFormat.replace(/\%h/, h);
timeFormat = timeFormat.replace(/\%m/, m);timeFormat = timeFormat.replace(/\%s/, s);$this.html(timeFormat);});});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){var d, h, m, s;$('div#reloj2').countdown(new Date(<?php echo $reloj2 ?>),function(event){var timeFormat ="%d dia(s) %h:%m:%s"
$this = $(this);switch(event.type){case "days":d = event.value;break;case "hours":h=event.value;break;case "minutes":
m = event.value;break;case "seconds":s = event.value;break;case "finished":$this.fadeTo('slow', 0.5);break;}
if(d > 0){timeFormat = timeFormat.replace(/\%d/, d);timeFormat = timeFormat.replace(/\(s\)/, Number(d) == 1 ? '' : 's');
}else{timeFormat = timeFormat.replace(/%d dia\(s\)/, '');}timeFormat = timeFormat.replace(/\%h/, h);
timeFormat = timeFormat.replace(/\%m/, m);timeFormat = timeFormat.replace(/\%s/, s);$this.html(timeFormat);});});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){var d, h, m, s;$('div#reloj3').countdown(new Date(<?php echo $reloj3 ?>),function(event){var timeFormat ="%d dia(s) %h:%m:%s"
$this = $(this);switch(event.type){case "days":d = event.value;break;case "hours":h=event.value;break;case "minutes":
m = event.value;break;case "seconds":s = event.value;break;case "finished":$this.fadeTo('slow', 0.5);break;}
if(d > 0){timeFormat = timeFormat.replace(/\%d/, d);timeFormat = timeFormat.replace(/\(s\)/, Number(d) == 1 ? '' : 's');
}else{timeFormat = timeFormat.replace(/%d dia\(s\)/, '');}timeFormat = timeFormat.replace(/\%h/, h);
timeFormat = timeFormat.replace(/\%m/, m);timeFormat = timeFormat.replace(/\%s/, s);$this.html(timeFormat);});});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



</script>


un bloque de codigo para cada reloj

Última edición por Mndrake; 20/12/2012 a las 14:45