Высокая загрузка процессора из-за Ajax Shoutbox

Хорошо, поэтому я сделал крикбокс, я использовал его в течение некоторого времени, но совсем недавно я обратил на это внимание, что он ест при использовании процессора из-за большого количества вызовов сценария, и мне интересно, как я могу приступить к фиксации его производительности.

Вот сценарий js для крикбокса.

var current_shouts = 0; function $shoutid(eleid) { return document.getElementById(eleid); } function urlencode(u) { u = u.toString(); var matches = u.match(/[\x90-\xFF]/g); if (matches) { for (var mid = 0; mid < matches.length; mid++) { var char_code = matches[mid].charCodeAt(0); u = u.replace(matches[mid], '%u00' + (char_code & 0xFF).toString(16).toUpperCase()); } } return escape(u).replace(/\+/g, "%2B"); } function shouts() { clearTimeout(getshout); var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("GET", "shoutbox/shouts.php?i=" + Math.random()); xmlHttp.onreadystatechange = function() { if (this.readyState == 4) { if (parseInt(this.responseText) > current_shouts) { getshouts(); current_shouts = parseInt(this.responseText); } getshout = setTimeout("shouts()", 1000); } } xmlHttp.send(null); } function getshouts() { var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("GET", "shoutbox/getshouts.php?i=" + Math.random()); xmlHttp.onreadystatechange = function() { if (this.readyState == 4) $shoutid("shoutbox").innerHTML = this.responseText; $shoutid("shoutbox").scrollTop = $shoutid("shoutbox").scrollHeight; } xmlHttp.send(null); } function push_shout() { shout(); return false; } function shout() { var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP"); xmlHttp.open("POST", "shoutbox/shout.php"); var data = "user=" + urlencode($shoutid("user").value) + "&" + "shout=" + urlencode($shoutid("shout").value); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlHttp.setRequestHeader("Content-length", data.length); xmlHttp.onreadystatechange = function() { if (this.readyState == 4) { if (!this.responseText) $shoutid("shout").value = ""; else { alert(this.responseText); } getshouts(); } } xmlHttp.send(data); return true; } var getshout = setTimeout("shouts()", 1000); 

Я не самый яркий карандаш, когда дело доходит до js, поэтому я не совсем уверен, как это исправить. Призыв к shouts.php – это то, что действительно есть в моем процессоре.

Вот сценарий для shouts.php

 <?php $db = new mysqli('localhost', 'username', 'pass', 'database'); if($db->connect_errno > 0){ die('Unable to connect to database [' . $db->connect_error . ']'); } $stmt = $db->query("SELECT COUNT(id) FROM shout"); while ($shout = $stmt->fetch_assoc()) { echo implode($shout); } session_write_close(); ?> 

Я прочитал о проблеме блокировки сессии, поэтому добавил в session_write_close(); но это, похоже, не помогает в моей проблеме.

Любые советы будут очень признательны!