【发布时间】:2011-03-25 16:59:32
【问题描述】:
这可能是一个愚蠢的问题,但为什么下面的 for 循环在 Chrome 中进入无限循环,而在 Firefox 中却没有? (显然,循环测试是它失败的地方——我只是不知道为什么)。
for(var i = 0; localStorage[this.config.localStoragePrefix + i] != 'undefined'; i++)
this.config.appCount++;
它正在检查存在多少 localStorage 元素。例如:
localStorage['myPrefix_0']
localStorage['myPrefix_1']
localStorage['myPrefix_2'] ...
将返回 3。
关于为什么这会在 Chrome 中永远循环的任何想法?
【问题讨论】:
-
有什么理由不能只使用
localStorage.length? -
删除 'undefined' 周围的引号后会发生什么?
'undefined' != undefined.
标签: javascript html for-loop local-storage