admin管理员组文章数量:1392007
I have a function that has a for statement that repeats until a variable (i) is equal to localStorage.length
and inside the for loop there is an if statement that checks if a localStorage entry starts with a # then runs a function. When I call this function nothing that is in the for loop runs.
function refresh() {
var i = 0;
console.info("The LocalStorage Length is: " + localStorage.length);
console.info("i is: " + i);
console.log("i == localStorage.length is:" + i == localStorage.length);
for (i = 0; i == localStorage.length; i++) {
console.info("i is: " + i);
current = localStorage.key(i);
if (current.substr(0, 1) == "#") {
noteArea.innerHTML = +createHtml(current.substr(1), localStorage.getItem(current)); //Adds the html to the page
console.log(current.substr(1) + " - Note displayed."); //Logs the note title
} else {
console.log("No notes saved.");
}
console.info("i == localStorage.length is:" + i == localStorage.length);
}
}
function createHtml(name, desc) {
var html = "<div class = 'note'> <h4 class = 'note-title'>" + name + "</h5> <p class = 'note-desc'>" + desc + "</p> </div> <br/>";
console.log(html);
return html;
}
I have a function that has a for statement that repeats until a variable (i) is equal to localStorage.length
and inside the for loop there is an if statement that checks if a localStorage entry starts with a # then runs a function. When I call this function nothing that is in the for loop runs.
function refresh() {
var i = 0;
console.info("The LocalStorage Length is: " + localStorage.length);
console.info("i is: " + i);
console.log("i == localStorage.length is:" + i == localStorage.length);
for (i = 0; i == localStorage.length; i++) {
console.info("i is: " + i);
current = localStorage.key(i);
if (current.substr(0, 1) == "#") {
noteArea.innerHTML = +createHtml(current.substr(1), localStorage.getItem(current)); //Adds the html to the page
console.log(current.substr(1) + " - Note displayed."); //Logs the note title
} else {
console.log("No notes saved.");
}
console.info("i == localStorage.length is:" + i == localStorage.length);
}
}
function createHtml(name, desc) {
var html = "<div class = 'note'> <h4 class = 'note-title'>" + name + "</h5> <p class = 'note-desc'>" + desc + "</p> </div> <br/>";
console.log(html);
return html;
}
Share
Improve this question
edited May 18, 2015 at 23:47
Chris D
asked May 18, 2015 at 23:37
Chris DChris D
131 gold badge1 silver badge5 bronze badges
6
- 4 Did you forget to post your code? – TimoStaudinger Commented May 18, 2015 at 23:38
- 4 Looks like he hit submit too soon, he was in the middle of a sentence. – Barmar Commented May 18, 2015 at 23:38
- Until you post your code, I can't help you – user4826496 Commented May 18, 2015 at 23:38
- Any code or clue what you are talking about? – Good Luck Commented May 18, 2015 at 23:39
-
What is the value of
localStorage.length
? – David Tansey Commented May 18, 2015 at 23:41
1 Answer
Reset to default 2Try changing your for statement so it says i < localStorage.length
.
Reason why your loop never executes is because the statement in the loop has to be TRUE
in order for it to execute. The loop executes while this statement remains TRUE
.
You can also remove the re-initialization of i
in your loop, because you already have a variable i
.
版权声明:本文标题:javascript - Why doesn't my 'for' statement loop and the if statement inside it execute? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772876a2624446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论