我正在尝试使用 jQuery 的高级自动完成功能,但我的脚本执行速度有问题。代码片段:
var data = <?php if(isset($names)) { echo json_encode(implode(" | ", array_unique($names))); } else { echo "null"; } ?>;
if (data != null) {
data = data.split(" | ");
$("#search_names").autocomplete(data);
}
我的数据来自一些 MySQL 表,在 jQuery 将其传递到输入字段之前由 PHP 处理。当我查看此类页面的源代码时,那里(显然)有大量文本,页面本身加载时间为 5-10 秒...
所以我想知道有没有办法以某种方式加速我的脚本?我知道 总是 有太多的文本要处理,无论是在同一个文件中还是在其他包含的文件中,但我只是想知道我是否因为数据太多而卡在 10 秒的加载页面上,或者我可以以某种方式让它更棒? :)
感谢您的帮助!
最佳答案
你绝对应该使用 remote autocomplete mechanism而不是每次都将所有数据填充到文档中。
The first argument can be an URL for remote data or an an array for local data.
For remote data: When the user starts typing, a request is send to the specified backend ("my_autocomplete_backend.php"), with a GET parameter named q that contains the current value of the input box and a parameter "limit" with the value specified for the max option.
如果查找仍然需要很长时间,您可能需要考虑优化 PHP 脚本而不是 jQuery 部分。诸如数据库使用索引等......
关于php - 优化 jQuery 速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5593167/