Advertisement
SuperMonkey

chat_javascript.js

Jul 2nd, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  lastMessageID = 0;
  2.  
  3.  function getXHR() {
  4.     var xhr = null;
  5.    
  6.     if (window.XMLHttpRequest || window.ActiveXObject) {
  7.         if (window.ActiveXObject) {
  8.             try {
  9.                 xhr = new ActiveXObject("Msxml2.XMLHTTP");
  10.             } catch(e) {
  11.                 xhr = new ActiveXObject("Microsoft.XMLHTTP");
  12.             }
  13.         } else {
  14.             xhr = new XMLHttpRequest();
  15.         }
  16.     }
  17.    
  18.     return xhr;
  19. }
  20.  
  21. function ajax_req_and_callback(page, callback) {
  22.     var xhr = getXHR();
  23.     xhr.open("GET", page);
  24.     xhr.send(null);
  25.     xhr.onreadystatechange = function() {
  26.         if(xhr.readyState == 4)
  27.             callback(xhr.responseText);
  28.     }
  29. }
  30.  
  31. function MaJSiNouveauxMessages() {
  32.     ajax_req_and_callback("messages.php?count", function(r) {
  33.         if(r > lastMessageID) {
  34.             ajax_req_and_callback("messages.php?depuis="+lastMessageID, function(a) {
  35.                 document.getElementById("messages").innerHTML += a;
  36.             });
  37.             lastMessageID = r;
  38.         }
  39.     });
  40. }
  41.  
  42.  
  43. var updateMsgTimer = window.setInterval(MaJSiNouveauxMessages, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement