Allora, volevo utilizzare questo script per creare una pagina che viene aggiornata ogni minuto. Purtroppo io non conosco bene AJAX nè Javascript, ho preso degli script trovati sulla rete e li ho riadattati per il mio uso...però mi rendo conto che non conoscendo a fondo (anzi non conoscendolo per nulla) AJAX, non so come risolvere un problema che ho con questo codice...
Il codice richiede l'orario attuale e quando deve essere il prossimo aggiornamento dal server, salvandoli nelle variabili current e next, dopodichè fa partire il timer, quando il timer finisce invia una richiesta alla pagina changeweather.php che se viene fatta nel momento corretto (cioè se è passato un minuto dall'ultima richiesta) dovrebbe aggiornare dei valori sul database e restituirli alla pagina che ha fatto la richiesta, se invece non è ancora passato il minuto allora restituisce i valori che sono attualmente salvati sul Database.
Purtroppo però se tengo diverse finestre aperte con la pagina contenente questo script, non tutte vengono aggiornate.
Credo che il problema sia nel fatto che le richieste sono asincrone, ma onestamente non so che fare.
Vi prego se possibile, di dare un'occhiata al codice (e di perdonarmi per gli orrori che posso aver scritto modificando gli script che già avevo
)
codice:
// <![CDATA[
var current;
var next;
var interval=60000;
var GetServerTimeUrl = "gettime.php";
var GetChaturl = "changeweather.php";
function getTimer(){
var times=new Array();
var j=1;
var i=1;
for(i=1; j<60; i++){
times[i]=j;
j=j+1;
}
if(next < current){
var later = new Date(current*1000)
}
else {
var later = new Date(next*1000);
}
var laterh = later.getMinutes();
j=0;
i=0;
while (i<times.length && j==0){
if (laterh<times[i]){
later.setMinutes(times[i], 0, 0);
j=1;
}
i++;
}
if(j==0){
later.setMinutes(59, 0, 0);
later.setTime(later.getTime() + 60000 + interval*1000);
}
var timer=Math.floor(next-current);
next=(later.getTime()/1000);
if (timer<0){
timer=0;
}
return timer;
}
function $id(id) {
return document.getElementById(id);
}
var countdown = function(seconds, formid, callback) {
var _ore = $id(formid + '_ore');
var _min = $id(formid + '_min');
var _sec = $id(formid + '_sec');
var _writecounter = function(s) {
var hh = Math.floor(s / 3600);
var mm = Math.floor((s - (hh*3600)) / 60);
var ss = s - (hh*3600) - (mm*60);
_ore.innerHTML=(hh < 10)? "0" + hh : hh;
_min.innerHTML=(mm < 10)? "0" + mm : mm;
_sec.innerHTML=(ss < 10)? "0" + ss : ss;
};
(function() {
_writecounter(seconds);
while (seconds--) return setTimeout(arguments.callee, 1000)
return callback();
})();
};
function initJavaScript() {
function execute(){
document.getElementById('loading').innerHTML="Caricamento...";
var now = new Date();
var seed = now.getTime();
var rand=Math.floor(1000+Math.random(seed)*1000);
window.setTimeout("receiveChatText()", rand);
window.setTimeout("removeLoad()", rand);
}
function removeLoad(){
document.getElementById('loading').innerHTML="";
}
//initiates the first data query
function receiveChatText() {
if (httpReceiveServerTime.readyState == 4 || httpReceiveServerTime.readyState == 0) {
httpReceiveServerTime.open("GET", GetServerTimeUrl, true);
httpReceiveServerTime.send(null);
httpReceiveServerTime.onreadystatechange = handlehHttpReceiveServerTime;
}
if (httpReceiveChat.readyState == 4 || httpReceiveChat.readyState == 0) {
httpReceiveChat.open("POST",GetChaturl, true);
httpReceiveChat.setRequestHeader("content-type", "application/x-www-form-urlencoded");
httpReceiveChat.send("now=" + next);
httpReceiveChat.onreadystatechange = handlehHttpReceiveChat;
}
}
//deals with the servers' reply to requesting new content
function handlehHttpReceiveChat() {
if (httpReceiveChat.readyState == 4 && httpReceiveServerTime.status == 200) {
var results = httpReceiveChat.responseText.split('---'); //the fields are seperated by ---
if (results.length > 2) {
j=0;
for(i=0;i < (results.length-1);i=i+2) { //goes through the result one message at a time
insertNewContent(results[i],results[i+1],j); //inserts the new content into the page
j++;
}
}
}
}
//inserts the new content into the page
function insertNewContent(liCond,liTemp,liId) {
document.getElementById('cond' + liId).innerHTML=liCond;
document.getElementById('temp' + liId).innerHTML=liTemp;
}
//deals with the servers' reply to requesting new content
function handlehHttpReceiveServerTime() {
if (httpReceiveServerTime.readyState == 4 && httpReceiveServerTime.status == 200) {
var times = httpReceiveServerTime.responseText.split('---'); //the fields are seperated by ---
current=times[1];
next=times[0];
countdown(getTimer(), 'cd1', function() { execute(); })
}
}
//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
// initiates the two objects for sending and receiving data
var httpReceiveChat = getHTTPObject();
var httpReceiveServerTime = getHTTPObject();
var httpSendChat = getHTTPObject();
// ]]>