gpt4 book ai didi

Javascript 数组操作 : Is there a better way to do the following?

转载 作者:行者123 更新时间:2023-11-29 15:04:00 25 4
gpt4 key购买 nike

我有以下代码:

  1. 从页面上的元素中抓取文本
  2. 过滤特定元素
  3. 在 '\n' 处拆分
  4. 删除数组中所有的空白元素

这似乎比我希望的要多花一点时间,并且不会像您预期的那样删除数组中所有空白填充的元素。

仅供引用,然后我将两个数组合并为一个并使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com