我有以下代码,它的功能是:
- 从页面上的元素中获取文本
- 过滤出特定的元素
- 按 '\n' 分割
- 删除数组中所有的空白元素
这似乎比我想象的要花费更多的时间,并且不能删除数组中填充了空格的所有元素。
仅供参考,我将两个数组合并成一个,并使用YepNope加载脚本和样式。这个过程需要1.5秒,对于用户来说等待时间太长了。
如何提高速度?
var $containerHtml = $(html);
// Add the scripts
scriptArray = $.grep($containerHtml.filter('#includeScripts').text().split('\n'), function (element, index) {
return element !== "" && element !== " ";
});
// Add the styles
styleArray = $.grep($containerHtml.filter('#includeStylesheets').text().split('\n'), function (element, index) {
return element !== "" && element !== " ";
});
// Combine the two arrays into 1
combinedArrays = scriptArray.concat(styleArray);
// Load the scripts and styles
yepnope([{
load: combinedArrays,
callback: function (url, result, key) {
if (window.console && window.console.firebug) {
console.log("Loaded " + url);
}
}}]);