Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/06/2013, 19:57
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 17 años, 7 meses
Puntos: 1567
Respuesta: 2 ajax no funcionan

No hay ningún impedimento para hacer varias llamadas simultaneas a Ajax, ni siquiera sería necesario utilizar el evento onload.

Este ejemplo es algo rebuscado, pero hace 3 llamadas a diferentes intervalos al mismo archivo, aunque podrían ser diferentes
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>titulo</title> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
div{
width: 300px;
height: 20px;
padding: 5px;
margin: 2px;
border: dotted 1px #666;
}
</style>
<script type="text/javascript">
//<![CDATA[
// agrega un parametro unico al archivo
var prevenirCache=1;
// innerHtml para imagen o texto Ajax en espera
var loaderGif = "<p style='font-size:10pt;display:inline-block; text-align: center; margin-top: 25%;'><img src='ajax-loader.gif' alt='Cargando datos' \/><br \/> Aguarde por favor...<\/p>";

/*** no editar ***/
var cargarObjetos="";
var rootdomain="http://"+window.location.hostname;
var cacheParam="";
function cargarAjax(url,divAjax){
var contenidoAjax = false;

if(window.XMLHttpRequest) {
contenidoAjax = new XMLHttpRequest();
}else if(window.ActiveXObject) {
contenidoAjax = new ActiveXObject("Microsoft.XMLHTTP");
}else{
alert('Su navegador no soporta Ajax');
}

contenidoAjax.onreadystatechange=function(){
cargaPagina(contenidoAjax,divAjax);
}
                
if(prevenirCache == 1){
    cacheParam=(url.indexOf("?")!=-1)? "&"+ new Date().getTime() : "?"+ new Date().getTime();
    }    
        contenidoAjax.open('GET', url+cacheParam, true);
    contenidoAjax.send(null);
    }
    
    
    function cargaPagina(contenidoAjax,divAjax){
        if(contenidoAjax.readyState==1){
        //    document.getElementById(divAjax).innerHTML = loaderGif;
            }
            if(contenidoAjax.readyState == 4 && (contenidoAjax.status==200 || window.location.href.indexOf("http")==-1)){
                document.getElementById(divAjax).innerHTML=contenidoAjax.responseText;
                }
                }
                

//]]>
</script>
</head>
<body>
<div>
Actualiza cada 1'' &nbsp;
<span id="uno">
<?php include('hora.php'); ?>
    <script type="text/javascript">
        setInterval("cargarAjax('hora.php','uno')", 1000);
        </script>
</span>
</div>
<div>
Actualiza cada 5'' &nbsp;
<span id="dos">
<?php include('hora.php'); ?>
        <script type="text/javascript">
        setInterval("cargarAjax('hora.php','dos')", 5000);
        </script>
</span>
</div>
<div>
Actualiza cada 10'' 
<span id="tres">
<?php include('hora.php'); ?>
        <script type="text/javascript">
        setInterval("cargarAjax('hora.php','tres')", 10000);
        </script>
</span>
</div>
</body>
</html>
hora.php

Código PHP:
<?php
echo date('H:i:s');
?>
Demo:
http://foros.emprear.com/ajax/basico...iple/index.php

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.