Ciao ragazzi,
mi potete dare una mano col ajax a dare una sistemata al codice....
se mi date una mano posto il codice....
Printable View
Ciao ragazzi,
mi potete dare una mano col ajax a dare una sistemata al codice....
se mi date una mano posto il codice....
Se non riesci con ajax (ci si incasina parecchio), prova ad usare jQuery!.. in particolare questo per ajax...
Ciao!
Ci riesco pił o meno ma il problema che vorrei dare pił ordine al codice...
1)Il problema principale e che vorrei fare che ogni 5 secondi la funzione
UpdateTimer(); si attivi e che ogni 20 secondi si attivi
questa parte di codice per una durata di 3 secondi....
2)Ma non capisco a cosa serva questa parte di codice....Codice PHP:document.getElementById("discussione").innerHTML = '<center><img src="img/loading.gif" alt="Attendere"><br>Elaborazione dati in corso...</center>';
cosa fa la function doReload....
Codice JS
Codice PHP:<script type="text/javascript">
var httpObject = null;
var link = "";
var timerID = 0;
var nickName = "<?php echo $_COOKIE['nickname']; ?>";
// Get the HTTP Object
function getHTTPObject(){
if(window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest) return new XMLHttpRequest();
else{
alert("Il tuo Browser non supporta AJAX.");
return null;
}}
// Change the value of the outputText field
function setOutput(){
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("discussione");
objDiv.innerHTML += response;
objDiv.scrollTop = objDiv.scrollHeight;
var inpObj = document.getElementById("messaggio");
inpObj.value = "";
inpObj.focus();
}}
// Change the value of the outputText field
function setAll(){
if(httpObject.readyState == 4){
var response = httpObject.responseText;
var objDiv = document.getElementById("discussione");
objDiv.innerHTML = response;
objDiv.scrollTop = objDiv.scrollHeight;
}}
// Implement business logic
function doWork(){
if(document.getElementById('messaggio').value < 1){
}else{
httpObject = getHTTPObject();
if(httpObject != null){
link = "message.php?nickname="+nickName+"&messaggio="+document.getElementById('messaggio').value;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = setOutput;
httpObject.send(null);
}}}
// Implement business logic
function doReload(){
httpObject = getHTTPObject();
var randomnumber=Math.floor(Math.random()*10000);
if(httpObject != null){
link = "message.php?all=1&rnd="+randomnumber;
httpObject.open("GET", link , true);
httpObject.onreadystatechange = setAll;
httpObject.send(null);
}}
function UpdateTimer(){
doReload();
timerID = setTimeout("UpdateTimer()", 5000);
document.getElementById("discussione").innerHTML = '<center><img src="img/loading.gif" alt="Attendere"><br>Elaborazione dati in corso...</center>';
}
function keypressed(e){
if(e.keyCode=='13'){
doWork();
}}
</script>
</head>
<body onLoad="UpdateTimer();">