Guest

WebApp Lag Meter non SSI using Ajax

By: a guest on Mar 31st, 2010  |  syntax: JavaScript  |  size: 1.94 KB  |  hits: 320  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <!--
  2.         For Help/Support
  3.         Go here: http://thegeekoftheworld.com/webapp-lag-meter-non-ssi-using-ajax/
  4.     This program is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program.  If not, see <http://www.gnu.org/licenses/>
  16.         -->
  17. <script>
  18. function createRequestObject() {
  19.   var ro;
  20.   var browser = navigator.appName;
  21.   if (browser == "Microsoft Internet Explorer") {
  22.     ro = new ActiveXObject("Microsoft.XMLHTTP");
  23.   } else {
  24.     ro = new XMLHttpRequest();
  25.   }
  26.   return ro;
  27. }
  28. var xmlHttp = createRequestObject();
  29. function runping() {
  30.         xmlHttp.open('get', '/?ajaxping&ms=' + new Date().getTime());
  31.         xmlHttp.onreadystatechange = readping;
  32.         xmlHttp.send(null);
  33. }
  34. function readping() {
  35.         if (xmlHttp.readyState == 4) {
  36.                 var a = new Date().getTime();
  37.                 var ping = a - b - 10;
  38.                 if (ping > 350) {
  39.                 var pc = 'red';
  40.                 var pcode = 'Ultra High lag';
  41.                 }
  42.                 if (ping <= 350) {
  43.                 var pc = 'orange';
  44.                 var pcode = 'High lag';
  45.                 }
  46.                 if (ping <= 225) {
  47.                 var pc = 'yellow';
  48.                 var pcode = 'Low lag';
  49.                 }
  50.                 if (ping <= 110) {
  51.                 var pc = 'green';
  52.                 var pcode = 'No lag';
  53.                 }
  54.                 if (document.getElementById("ping")) {
  55.                         document.getElementById("ping").innerHTML = ping + 'ms<br /><font color="' + pc + '">' + pcode + '</font>';
  56.                 }
  57.         }
  58.         //setTimeout("var b = new Date().getTime(); runping();", 5000); // Has a Error in it for testing only
  59. }
  60. var b = new Date().getTime();
  61. runping();
  62. </script>
  63.  
  64. <div id="ping"></div>