作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
这似乎比我希望的要多花一点时间,并且不会像您预期的那样删除数组中所有空白填充的元素。
仅供引用,然后我将两个数组合并为一个并使用 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);
}
}}]);
最佳答案
为什么在您的 html 页面中将脚本作为文本数组?
我会将它们存储为服务器上的 .json
文件。
$.when($.getJSON("url/script.json"), $.getJSON("url/stylesheet.json")).then(function(script, style) {
var arr = ...; // combine script & style.
yepnope(...);
});
如果您不希望它们是静态的,那么可以根据传入的 URL 设置一些路由和服务器 json 文件。
关于Javascript 数组操作 : Is there a better way to do the following?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5651600/
我是一名优秀的程序员,十分优秀!