Znalazłem gdzieś na googlach tutorial jak w prosty i szybki sposób napisać shoutbox`a w ajax. Po wprowadzeniu kilku modyfikacji dodałem go do forum. Niestety okazał się on pamięciożerny. O ile forum zabierało średnio 1,5% zużycia serwera to przez tego shoutbox`a brał 5+%
Czy da się go jakoś zoptymalizować?
A może znacie jakiś "lżejszy"?
<?php
var check=0;
function ajaxFunction(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
//browsers all not support, rare case
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}
function showData() {
htmlRequest = ajaxFunction();
if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request");
return;
}
htmlRequest.onreadystatechange = function(){
if(htmlRequest.readyState == 4){
document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
if(!check)
{
check=1;
showBottom();
}
}
}
htmlRequest.open("GET", "shout/outputinfo.php", true);
htmlRequest.send(null);
}
showData();
setInterval("showData()",10000);
function saveData(){
htmlRequest = ajaxFunction();
if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request");
return;
}
htmlRequest.open('POST', 'shout/sendshout.php');
htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
htmlRequest.send('name='+document.shoutbox.shouter.value+'&message='+document.shoutbox.shouter_comment.value);
document.shoutbox.shouter_comment.value = ''; // Updates the shout box’s text area to NULL.
document.shoutbox.shouter_comment.focus(); // Focuses the text area.
showData();
showBottom();
}
function removeData(kokos){
htmlRequest = ajaxFunction();
if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
alert ("Browser does not support HTTP Request");
return;
}
htmlRequest.open('POST', 'shout/removeshout.php');
htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
htmlRequest.send('koko='+kokos);
showData();
showBottom();
}
function showBottom() {
document.getElementById("shoutarea").scrollTop = document.getElementById("shoutarea").scrollHeight;
}
?>
Ten post edytował Siela 28.05.2008, 08:11:50