我有以下代码:
的元素。
这似乎比我所希望的要花费更多的时间,并且没有像您所期望的那样删除数组中所有填充空白的元素。
作为参考,我将这两个数组组合为一个,并使用YepNope加载脚本和样式。这个过程大约需要1.5s,这对用户来说是很长的等待时间。
我怎样才能提高速度?
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);
}
}}]);
发布于 2011-04-13 15:23:56
为什么在html页面中使用脚本作为文本数组?
我会将它们作为.json
文件存储在服务器上。
$.when($.getJSON("url/script.json"), $.getJSON("url/stylesheet.json")).then(function(script, style) {
var arr = ...; // combine script & style.
yepnope(...);
});
如果不希望它们是静态的,那么根据传入的URL设置一些路由和服务器json文件。
https://stackoverflow.com/questions/5651600
复制相似问题