通过JS存储和访问随机文本的最有效/最高效的方法是什么?

时间:2013-04-10 22:44:59

标签: javascript arrays html5 cordova random

我正在使用HTML5 / jQuery + PhoneGap构建一个回合制的iPad游戏。

玩家每转一圈,我们想要显示一些随机的基于文本的信息。通常一句话。

总的来说,我们可能有大约100个不同的句子分为4组。

就性能而言,采用这种方法的正确方法是什么?

这些内容是否适合简单地存储在4个javascript数组中?或者那太多了?如果太多,是否会创建一个单独的JSON文件更有意义?还是使用本地存储?或者那些是否有点矫枉过正?或者我们应该在HTML DOM中“存储”100位文本并从那里访问它?其他想法?

2 个答案:

答案 0 :(得分:0)

使用localStorage并将25存储在一个由任何字符分隔的localStorage对象中(例如“^”),然后使用split()来访问字符串。

localStorage.str = "foo^bar^baz"; //etc..
var str1 = localStorage.str;
str1.split("^");
for(var i = 0; i < str.length;i++){
alert(str1[i])
}

答案 1 :(得分:0)

100个句子是微不足道的,使用数组不会对性能产生任何影响,但你可以随时尝试...

http://jsfiddle.net/ZpCfB/(100)

http://jsfiddle.net/ZpCfB/1/(4000)

var sentences = [[], [], [], []],
    groupCount = 4,
    i = 0,
    sentence = 'This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence This is a very very very long sentence sdfsdfsdf sdfsdfsdfsdf sdfsd sdfsdfsdfsdf sdfsd fsdfsdfsdf s.';

for (; i < 25; i++) {
    for (x = 0; x < 4; x++) {
        sentences[x].push(sentence);
    }
}

console.log(sentences);