我认为 jylauril 走在正确的轨道上。
我一直在玩延迟,并注意到一些奇怪的行为。
据我所知,似乎如果您在所有 JavaScript 完成执行之前触摸 localStorage,包括 JS 执行 setTimeout,localStorage 对于该网页浏览将显示为空白。例如:
$(window).load(function () {
console.log("Waiting 10000ms", new Date());
setTimeout(setDeckSelect, 10000);
});
Firebug 的控制台:
Waiting 10000ms Date {Thu Dec 13 2012 10:35:48 GMT-0500 (Eastern Standard Time)}
exec.js (line 191)
getDecks Date {Thu Dec 13 2012 10:35:58 GMT-0500 (Eastern Standard Time)}
vault.js (line 23)
>>> localStorage
0 items in Storage
我想我可能会做一些事情,但到目前为止我的理论被证明是错误的。不过,我注意到了一件奇怪的事情。无论我等待多长时间,如果我先尝试查找套牌,它将失败并且本地存储将是空的:
>>> vault.getDecks()
[]
>>> localStorage
0 items in Storage
但是如果我按照相反的顺序做这些......
>>> localStorage
8 items in Storage deck:dfs=
"{"identity":"MakingNews","cards":{}}", deck:ngrngfrn= "{"identity":"BuildingaBetterWorld","cards":{}}", deck:sdfgshsh= "{"identity":"MakingNews","cards":{}}", deck:sdfgdgdfg= "{"identity":"MakingNews","cards":{}}", deck:dfgdfgas= "{"identity":"EngineeringtheFuture","cards":{}}", deck:sdfsga= "{"identity":"MakingNews","cards":{}}", deck:gdgd= "{"identity":"MakingNews","cards":{}}", deck:gfsdfgsdfg= "{"identity":"BuildingaBetterWorld","cards":{}}"
>>> vault.getDecks()
[Object { name= "dfgdfgas", key= "deck:dfgdfgas"}, Object { name= "dfs", key= "deck:dfs"}, Object { name= "gdgd", key= "deck:gdgd"}, Object { name= "gfsdfgsdfg", key= "deck:gfsdfgsdfg"}, Object { name= "ngrngfrn", key= "deck:ngrngfrn"}, Object { name= "sdfgdgdfg", key= "deck:sdfgdgdfg"}, Object { name= "sdfgshsh", key= "deck:sdfgshsh"}, Object { name= "sdfsga", key= "deck:sdfsga"}]
如果我在函数中记录 localStorage,它也可以:
vault.getDecks = function () {
console.log(localStorage);
var keys = Object.keys(localStorage),
out = [],
i,
k,
name = "";
for (i = 0; i < keys.length; i++) {
k = keys[i];
name = vault.friendlyName(k);
if (name !== k && localStorage[k]) {
out.push({name: name, key: k});
}
}
out.sort(function (a, b) {
return a.name > b.name ? 1 : -1;
});
return out;
};
有用。如果我取消它,虽然...
vault.getDecks = function () {
// console.log(localStorage);
void localStorage;
var keys = Object.keys(localStorage),
out = [],
i,
k,
name = "";
for (i = 0; i < keys.length; i++) {
k = keys[i];
name = vault.friendlyName(k);
if (name !== k && localStorage[k]) {
out.push({name: name, key: k});
}
}
out.sort(function (a, b) {
return a.name > b.name ? 1 : -1;
});
return out;
};
这没用。如果我删除 void 关键字并仅将 localStorage 作为语句,它也不起作用。
我不知道为什么,但 console.log(localstorage) 似乎确实解决了这个问题,我可以随时调用 localStorage。
实在是太诡异了。
编辑:我找到了一个更好的解决方案。调用 localStorage 的 'length' 属性也可以。
vault.getDecks = function () {
//Weird hack to make FF load localStorage correctly...
localStorage.length;
var keys = Object.keys(localStorage),
out = [],
i,
k,
name = "";
for (i = 0; i < keys.length; i++) {
k = keys[i];
name = vault.friendlyName(k);
if (name !== k && localStorage[k]) {
out.push({name: name, key: k});
}
}
out.sort(function (a, b) {
return a.name > b.name ? 1 : -1;
});
return out;
};
这更好一点,因为它不记录任何东西......