PHP - Ejecutar script con ajax

 
Vista:
sin imagen de perfil

Ejecutar script con ajax

Publicado por Isabel (11 intervenciones) el 02/12/2014 20:16:23
Hola a todos! Hace un par de semanas, escribí preguntando cómo podía actualizar parte de mi web hecha en php) sin refrescar la página, me indicastéis node.js, websocket, y ajax. Al final encontré un ejemplo con Ajax perfecto para mi caso en la siguiente dirección: http://vcomputadoras.com/refrescar-diferentes-divs-con-javascript-y-ajax/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<script type="text/javascript" language="javascript">
function refreshDivs(divid,secs,url)
{
 
// define our vars
var divid,secs,url,fetch_unix_timestamp;
 
// Chequeamos que las variables no esten vacias..
if(divid == ""){ alert('Error: escribe el id del div que quieres refrescar'); return;}
else if(!document.getElementById(divid)){ alert('Error: el Div ID selectionado no esta definido: '+divid); return;}
else if(secs == ""){ alert('Error: indica la cantidad de segundos que quieres que el div se refresque'); return;}
else if(url == ""){ alert('Error: la URL del documento que quieres cargar en el div no puede estar vacia.'); return;}
 
// The XMLHttpRequest object
 
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Tu explorador no soporta AJAX.");
return false;
}
}
}
 
// Timestamp para evitar que se cachee el array GET
 
fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}
 
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
 
// the ajax call
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout(function(){refreshDivs(divid,secs,url);},secs*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}
 
// LLamamos las funciones con los repectivos parametros de los DIVs que queremos refrescar.
window.onload = function startrefresh(){
refreshDivs('div1',5,'div1.php');
refreshDivs('div2',3,'div2.php');
}
</script>
, pero ahora me encuentro con un problema: Uno de los divs que necesito que se actualice solo, es un crono cuenta atrás hecho con JavaScript (se debe actualizar en tiempo real porque es posible aumentar el tiempo en los últimos minutos), ¿Cómo puedo hacer que lo ejecute? Si alguien me puede ayudar, se lo agradezco mucho!!!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder