Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [AJAX] Shoutbox
Siela
post
Post #1





Grupa: Zarejestrowani
Postów: 27
Pomógł: 1
Dołączył: 28.02.2005
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


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"?

  1. <?php
  2. var check=0;
  3.  function ajaxFunction(){
  4. var ajaxRequest;
  5.  
  6. try{
  7. // Opera 8.0+, Firefox, Safari
  8. ajaxRequest = new XMLHttpRequest();
  9. } catch (e){
  10. // Internet Explorer Browsers
  11. try{
  12. ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  13. } catch (e) {
  14. try{
  15. ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  16. } catch (e){
  17. //browsers all not support, rare case
  18. alert("Your browser broke!");
  19. return false;
  20. }
  21. }
  22.  
  23. }
  24. return ajaxRequest;
  25. }
  26.  
  27.  
  28. function showData() {
  29. htmlRequest = ajaxFunction();
  30. if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
  31. alert ("Browser does not support HTTP Request");
  32. return;
  33. } 
  34.  
  35. htmlRequest.onreadystatechange = function(){
  36. if(htmlRequest.readyState == 4){
  37. document.getElementById("shoutarea").innerHTML = htmlRequest.responseText;
  38. if(!check)
  39. {
  40. check=1;
  41. showBottom();
  42. }
  43. }
  44. }
  45. htmlRequest.open("GET", "shout/outputinfo.php", true);
  46. htmlRequest.send(null);
  47.  
  48. }
  49.  
  50. showData();
  51. setInterval("showData()",10000);
  52.  
  53.  
  54. function saveData(){
  55. htmlRequest = ajaxFunction();
  56. if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
  57. alert ("Browser does not support HTTP Request");
  58. return;
  59. } 
  60.  
  61. htmlRequest.open('POST', 'shout/sendshout.php');
  62. htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  63. htmlRequest.send('name='+document.shoutbox.shouter.value+'&message='+document.shoutbox.shouter_comment.value); 
  64.  
  65. document.shoutbox.shouter_comment.value = ''; // Updates the shout box’s text area to NULL.
  66. document.shoutbox.shouter_comment.focus(); // Focuses the text area.
  67. showData();
  68. showBottom();
  69.  
  70. } 
  71. function removeData(kokos){
  72. htmlRequest = ajaxFunction();
  73. if (htmlRequest==null){ // If it cannot create a new Xmlhttp object.
  74. alert ("Browser does not support HTTP Request");
  75. return;
  76. } 
  77. htmlRequest.open('POST', 'shout/removeshout.php');
  78. htmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  79. htmlRequest.send('koko='+kokos); 
  80. showData();
  81. showBottom();
  82. } 
  83.  
  84. function showBottom() {
  85. document.getElementById("shoutarea").scrollTop = document.getElementById("shoutarea").scrollHeight;
  86. }
  87. ?>


Ten post edytował Siela 28.05.2008, 08:11:50
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 06:51