2

访问页面时,我的服务器 CPU 功率使用 11% / 100%,每个打开的选项卡都是另外 11%。这是我的 JScript 的代码:

function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('pop');
            ajaxDisplay.innerHTML = "Population: " + ajaxRequest.responseText;
        }
    }
    ajaxRequest.open("GET", "count.php", true);
    ajaxRequest.send();
}

这是PHP代码:

<?php
$con = @mysql_connect("31.xx.x.xxx","xxxxxxxx","xxxxxxxxxxxx");
if (!$con)
  {
  echo('?');
  }
@mysql_select_db("archstud_db", $con);
$result=mysql_query("SELECT * FROM chars");
$num=mysql_num_rows($result);
$num = $num;
echo $num;

@mysql_close($con);
?>

我还将 setInterval() 与我的 JScript 一起使用,window.onload 启动 setinterval 函数,每 6 秒刷新一次 JScript。

是什么占用了这么多CPU?另外,我该如何解决?

4

2 回答 2

1

The OP has not pasted the important part of the code, and requests to see it are unsuccessful, so I will hazard a guess and back it up with reasonable explanations. His server's CPU is being overloaded - this means that whatever happens, there's something pinging the server a gianormous amount of times. We also know that every tab adds 11% locally, which means that something is doing a gianormous amount of things in the browser.

The OP stated about setInterval. I am ready to bet that he has the following line, somewhere:

setInterval(ajaxFunction, 6);

Which, he may believe, pings every 6 seconds. In fact, it pings every six milliseconds, which would both explain the server-side and client-side load without freezing the page.

A quick workaround is the following:

setInterval(ajaxFunction, 6000);
于 2013-05-02T22:47:05.183 回答
-2

为什么您仍在使用已弃用的库(mysql)?

你听说过索引吗?“SELECT * FROM chars”可能会把事情搞砸。

任何你为什么每6秒刷新一次?

于 2013-05-02T03:29:46.030 回答