Advertisement
Thomas131

my custom modifications to the chat

Jul 2nd, 2015
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. My JS:
  2. ======
  3.     // jQuery Document
  4.     $(document).ready(function(){
  5.         //If user wants to end session
  6.         $("#exit").click(function() {
  7.             var exit = confirm("Are you sure you want to end the session?");
  8.             if(exit==true){window.location = 'index.php?logout=true';}     
  9.         });
  10.  
  11.         //If user submits the form
  12.         $("#submitmsg").click(function() { 
  13.             var clientmsg = $("#usermsg").val();
  14.             $.post("post.php", {text: clientmsg});             
  15.             $("#usermsg").attr("value", "");
  16.             return false;
  17.         });
  18.  
  19.         //Load the file containing the chat log
  20.         function loadLog(){    
  21.             var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
  22.             $.ajax({
  23.                 url: "log.php",
  24.                 cache: false,
  25.                 success: function(html) {      
  26.                     $("#chatbox").html(html); //Insert chat log into the #chatbox div  
  27.                    
  28.                     //Auto-scroll          
  29.                     var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
  30.                     if(newscrollHeight > oldscrollHeight){
  31.                         $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
  32.                     }
  33. /**
  34.  * Here, the change starts!!!
  35. /**/           
  36.                 },
  37.                 complete: function() {     
  38.                      setTimeout(loadLog, 500);             
  39.                 }
  40.             });
  41.         }
  42.         loadLog();
  43.     });
  44.  
  45. /**
  46.  * End of Javascript
  47. /**/
  48.  
  49.  
  50. I created log.php, too.
  51.  
  52. log.php:
  53. ========
  54. <?php
  55. while((!file_exists("log.html") || filemtime("log.html") < (time()-4) ) && time()%20) { //Don't change the 20 to a high number. Leave some space to 30. To high numbers can cause Network-Timeout-Errors
  56.     sleep(1);
  57.     clearstatcache(true,"log.html");
  58. }
  59.  
  60. echo file_get_contents("log.html");
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement