简体   繁体   English

尝试过多后,jQuery即时搜索停顿了

[英]Jquery instant search stalls out after too many attempts

After hammering on my instant search which calls to a LAMP SQL Server on GODADDY the search crashes and doesn't return any instant search results. 敲击我的即时搜索后,它调用了GODADDY上的LAMP SQL Server,搜索崩溃,并且不返回任何即时搜索结果。 What could possibly be going wrong? 可能出什么问题了? Can you crash a server after too many instant searches? 过多的即时搜索后,您能否使服务器崩溃?

The Script is here: 脚本在这里:

<form role="form" method="post" class="search">
    <input type="text" class="form-control" id="keyword" placeholder="Search for the perfect horse...">
</form></center>
<center><ul id="content"></ul></center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#keyword').on('input', function () {
            var searchKeyword = $(this).val();
            if (searchKeyword.length >= 0) {
                $.post('search2.php', { keywords: searchKeyword }, function (data) {
                    $('ul#content').empty()
                    $.each(data, function () {
                        $('ul#content').append('<a href="getgift2.php?id=' + this.Horse + '">' + this.Horse + '  ' + this.odds + '  ' + this.UTCTime + '  ' + this.state + '</a><br /><br />');
                    });
                }, "json");
            }
        });
    });
</script> 

if (!empty($_POST['keywords'])) {
$dateWindow=(int)$_POST['dateWindow'];

$keywords = $db->real_escape_string($_POST['keywords']);
$sql = "SELECT Horse, odds, UTCTime, state FROM Runners WHERE Horse LIKE '%".$keywords."%' and UTCTime > '".$d."' ORDER BY UTCTime ASC";
$result = $db->query($sql) or die($mysqli->error);
if ($result->num_rows > 0) {
    while ($obj = $result->fetch_object()) {
        $arr[] = array('Horse' => $obj->Horse, 'odds' => $obj->odds, 'UTCTime' => $obj->UTCTime, 'state' => $obj->state);
    }
}
}
echo  json_encode($arr) ;

Yes, instant search requests may cause the server to crash due to the quick requests being sent to server and performing a heavy mysql query to get the results. 是的,由于将快速请求发送到服务器并执行繁重的mysql查询以获取结果,因此即时搜索请求可能会导致服务器崩溃。 A good solution is to use jQuery throttle/debounce library to rate limit your function in order to avoid server crashing. 一个好的解决方案是使用jQuery节流/防反弹库对您的函数进行速率限制,以避免服务器崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM