Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Problemas con SetInterval() y setTimeout()

Estas en el tema de Problemas con SetInterval() y setTimeout() en el foro de Frameworks JS en Foros del Web. Buenasss. Que tal a todos gentes, como andan?? Bueno, les comento el problemita que tengo: Encare la idea de hacer un chat con archivos de ...
  #1 (permalink)  
Antiguo 07/12/2009, 08:32
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 6 meses
Puntos: 65
Problemas con SetInterval() y setTimeout()

Buenasss. Que tal a todos gentes, como andan??
Bueno, les comento el problemita que tengo:

Encare la idea de hacer un chat con archivos de texto (para hacerlo liviano) hace unos dias y hoy lo termine (o casi =P)
Logre el reconocimiento de usarios online y offline, los mensajes aparecen correctamente y todo.
Para hacer la busqueda de mensajes y usuarios nuevos, lo que hago es llamar algunos archivos PHP mediante AJAX que abren y leen los archivos de texto correspondientes.
Para hacer la consulta a cada rato use setTimeout() y setInterval(). El problema es que llega un momento en el que cada funcion que consulta a los PHP se ejecuta 10 veces seguidas sin motivo.
Son 3 funciones distintas, imaginense entonces 30 consultas a las vez a arcchivos PHP, demas esta decir que se super-cuelga el navegador.
Lo que no entiendo es porque pasa esto, si explicitamente le estoy pasando el tiempo de espera....

Bueno, agrego el codigo a ver si alguien tiene idea porque. Muchas gracias!!!

Código PHP:
//esta funcion llama al PHP para que lea el archivo de texto con nuevos mensajes
function newMsg(){
    var 
xhr;
    if(
window.XMLHttpRequest){
        
xhr=new XMLHttpRequest();
    }else if(
window.ActiveXObject){
        
xhr=new ActiveXObject("Microsoft.XMLHTTP");
    }
        var 
div=document.getElementById("chatScreen");
        
xhr.open("GET","newMsg.php");
        
xhr.onreadystatechange=function(){
            if(
xhr.readyState==4){
                if(
xhr.status==200){
                    var 
msg=xhr.responseText;
                    
div.innerHTML=msg+"<br /><br />";
                }
            }    
        }
xhr.send(null);
setTimeout("newMsg();",15000);
}

//esta funcion agrega a un archivo de texto los mensajes escritos
function addMsg(){
    var 
msg=document.getElementById("message");
    if(
msg.value!='Escribe aquí tu mensaje'){
        var 
xhr;
        if(
window.XMLHttpRequest){
            
xhr=new XMLHttpRequest();
        }else if(
window.ActiveXObject){
            
xhr=new ActiveXObject("Microsoft.XMLHTTP");
        }
            
xhr.open("GET","addMsg.php?msg="+msg.value);
            
xhr.onreadystatechange=function(){
                if(
xhr.readyState==4){
                    if(
xhr.status==200){
                        
msg.value="";
                    }
                }    
            }
        }
xhr.send(null);
}

//esta funcion llama al PHP que busca los conectados recientemente
function whosOnline(){
        var 
xhr;
    if(
window.XMLHttpRequest){
        
xhr=new XMLHttpRequest();
    }else if(
window.ActiveXObject){
        
xhr=new ActiveXObject("Microsoft.XMLHTTP");
    }   
        
xhr.open("GET","whosOnline.php");
        
xhr.onreadystatechange=function(){
            if(
xhr.readyState==4){
                if(
xhr.status==200){
                    var 
resp=xhr.responseText;
                    var 
div=document.getElementById("online");
                        
div.innerHTML=resp;
                    }
            }    
        }
xhr.send(null);
setTimeout("whosOnline();",20000);
}

// esta funcion abre el archivo PHP para ver quien se desconecto
function whosOffline(){
        var 
xhr;
    if(
window.XMLHttpRequest){
        
xhr=new XMLHttpRequest();
    }else if(
window.ActiveXObject){
        
xhr=new ActiveXObject("Microsoft.XMLHTTP");
    }
        var 
div=document.getElementById("OnLineUsers");
        
        
xhr.open("GET","whosOffline.php");
        
xhr.onreadystatechange=function(){
            if(
xhr.readyState==4){
                if(
xhr.status==200){
                    var 
rta=xhr.responseText;
                    var 
div=document.getElementById("offline");
                    
div.innerHTML=rta;
                }
            }    
        }
xhr.send(null);
setTimeout("whosOffline();",20000);

  #2 (permalink)  
Antiguo 07/12/2009, 13:06
Avatar de jackson666  
Fecha de Ingreso: noviembre-2009
Ubicación: Buenos Aires, Argentina
Mensajes: 1.971
Antigüedad: 14 años, 6 meses
Puntos: 65
Respuesta: Problemas con SetInterval() y setTimeout()

Ya esta solucionado, gracias por leer! =P
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:42.