Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/03/2011, 04:25
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: refrescar varios divs cada "x" segundos

Que tal memoadian,

Si lo miras con firebug te vas a dar cuenta de lo que sucede, estas sobreecribiendo el request, en realidad en tu caso precisas 1 objeto xmlhttprequest por peticion, deberias separar la creacion del objeto del updater, podria ser algo asi:

index.html
Código HTML:
Ver original
  1. <title>Refresca un div tag sin necesidad de refrescar toda la pagina</title>
  2.     <script src="ajax.js"></script>
  3. </head>
  4.     <div id="contenido">
  5.         <h3>Refrescar div's tag con Ajax</h3>
  6.         <div id="timediv">
  7.         </div>
  8.         <div  id="timediv2">
  9.         </div>
  10.         <div id="timediv3">
  11.         </div>
  12.         <div id="timediv4">
  13.         </div>
  14.     </div>
  15. </body>
  16. </html>

tiempo.php
Código PHP:
Ver original
  1. // Formateamos la salida de la variable.
  2.  
  3. $str = "It is %a on %b %d, %Y, %X - Time zone: %Z";
  4.  
  5. // Printeamos el resultado
  6.  
  7. echo (gmstrftime($str,time()));

ajax.js
Código Javascript:
Ver original
  1. function newXMLHttpRequest(){
  2.     var xmlHttp;
  3.     try{
  4.         xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
  5.     } catch (e){
  6.         try{
  7.             xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
  8.         } catch (e){
  9.             try{
  10.                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  11.             } catch (e){
  12.                 alert("Tu explorador no soporta AJAX.");
  13.                 return false;
  14.             }
  15.         }
  16.     }
  17.     return xmlHttp;
  18. }
  19.  
  20. var requests = {}
  21.  
  22. function refreshdiv(url, container, seconds){
  23.    
  24.     if(!requests[container]) requests[container] = newXMLHttpRequest();
  25.     var xmlHttp = requests[container];
  26.     // Timestamp for preventing IE caching the GET request
  27.     var fetch_unix_timestamp = '';// lets declare the variable
  28.  
  29.     fetch_unix_timestamp = function() {
  30.         return parseInt(new Date().getTime().toString().substring(0, 10))
  31.     }
  32.  
  33.     var timestamp = fetch_unix_timestamp();
  34.     var nocacheurl = url + '?t=' + timestamp;
  35.    
  36.     xmlHttp.onreadystatechange = function() {
  37.         if(xmlHttp.readyState==4){
  38.             document.getElementById(container).innerHTML = container + '::' + xmlHttp.responseText;
  39.             setTimeout(function(){refreshdiv(url, container, seconds)}, seconds * 1000);
  40.         }
  41.     }
  42.     xmlHttp.open('GET', nocacheurl, true);
  43.     xmlHttp.send(null);
  44. }
  45.  
  46.  
  47. window.onload = function startrefresh(){
  48.     refreshdiv('tiempo.php', 'timediv', 1);
  49.     refreshdiv('tiempo.php', 'timediv2', 3);
  50.     refreshdiv('tiempo.php', 'timediv3', 5);
  51.     refreshdiv('tiempo.php', 'timediv4', 8);
  52. }


lo que deberias hacer es cambiar las opciones en refreshdiv por las correspondientes a tus 4 peticiones.

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)