最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why doesn't my 'for' statement loop and the if statement inside it execute? - Stack Overflo

programmeradmin2浏览0评论

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
 |  Show 1 more ment

1 Answer 1

Reset to default 2

Try 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.

发布评论

评论列表(0)

  1. 暂无评论