看起来我的每个循环都循环不止一次

Looks like my each loop is looping more than once

本文关键字:循环 不止一次 我的 看起来      更新时间:2023-09-26

下面的代码应该从cookie中读取存储的ID(保存在cookie中,因此如果您重新加载页面,它将把您保存的会话存储在cookie中以便下次读取),查找链接到该ID的DIV,并将行附加到带有ID所针对的会话的标题,日期,代码和时间的表中, 但是,当有多个日期链接到单个会话 DIV 时,它应该为每个日期创建一行,这似乎是正确的。

但是,似乎sessDateVar.each(function( i, val )的每个循环都运行了两次?当我console.log while 循环中附加的行的当前会话代码和日期以及 i 的值时,它似乎重复了?我似乎想不通。

代码现在有点乱,对不起!如果我遗漏了什么,请告诉我!

while 循环运行时控制台的内容

(index):1994 Wrote a row for session A6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session D6 on date Thursday, May 19, 2016
(index):1995 1
(index):1994 Wrote a row for session A6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session D6 on date Thursday, May 19, 2016
(index):1995 1
(index):1994 Wrote a row for session C6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session F6 on date Thursday, May 19, 2016
(index):1995 1
(index):1994 Wrote a row for session C6 on date Wednesday, May 18, 2016
(index):1995 0
(index):1994 Wrote a row for session F6 on date Thursday, May 19, 2016
(index):1995 1

《守则》

 function writeTable() {
  if (checkCookie() === false) {
    $('#table-container').append('<table class="tg" id="session-table">'n<thead>'n<tr>'n<th class="tg-yw4l">Session</th>'n<th class="tg-yw4l">Date</th>'n<th class="tg-yw4l">Time</th>'n<th class="tg-yw4l"></th>'n</tr>'n</thead>'n<tbody>'n</tbody>'n</table>');
    $('.table-container-export').append('<table class="tg" cellpadding="10" id="session-table-export">'n<thead>'n<tr>'n<th class="tg-title">Session</th>'n<th class="tg-info">Date</th>'n<th class="tg-info">Time</th>'n<th class="tg-info">Session Code</th>'n</tr>'n</thead>'n<tbody>'n</tbody>'n</table>');
  } else if (checkCookie() === true) {
    var askToLoad = confirm('You have saved sessions in your browser, would you like to load those sessions?');
    if (askToLoad === true) {
      $('#table-container').append('<table class="tg" id="session-table">'n<thead>'n<tr>'n<th class="tg-yw4l">Session</th>'n<th class="tg-yw4l">Date</th>'n<th class="tg-yw4l">Time</th>'n<th class="tg-yw4l"></th>'n</tr>'n</thead>'n<tbody>'n</tbody>'n</table>');
      $('.table-container-export').append('<table class="tg" cellpadding="10" id="session-table-export">'n<thead>'n<tr>'n<th class="tg-title">Session</th>'n<th class="tg-info">Date</th>'n<th class="tg-info">Time</th>'n<th class="tg-info">Session Code</th>'n</tr>'n</thead>'n<tbody>'n</tbody>'n</table>');
      var tableDataUnparsed = Cookies.get('session-table');
      var tableDataExportUnparsed = Cookies.get('session-table-export');
      var tableDataParsed = JSON.parse(tableDataUnparsed);
      var tableDataExportParsed = JSON.parse(tableDataExportUnparsed);
      var tableDataExport = tableDataExportParsed;
      $.each(tableDataExportParsed, function( i, val ){
        var sessID = val;
        var sessionToFind = $(sessID);
        var sessTitle = sessionToFind.find('.title').html();
        var sessDateVar = sessionToFind.find('.date');
          if (sessDateVar.size() > 1) {
            var sessDates = [];
            sessDateVar.each(function() {
              sessDates.push($.trim($(this).html()));
            });
          } else {
            var sessDate = $.trim(sessionToFind.find('.date').html());
            var sessDates = false;
          }
        var sessTimeVar = sessionToFind.find('.time');
          if (sessTimeVar.size() > 1) {
            var sessTimes = [];
            sessTimeVar.each(function() {
              sessTimes.push($.trim($(this).html()));
            });
          } else {
            var sessTime = $.trim(sessionToFind.find('.time').html());
          }
        var sessCodeVar = sessionToFind.find('.code');
          if (sessCodeVar.size() > 1) {
            var sessCodes = [];
            sessCodeVar.each(function() {
              sessCodes.push($.trim($(this).html()));
            });
          } else {
            var sessCode = $.trim(sessionToFind.find('.code').html());
          }

        if (sessDates === false) {
          $('#session-table-export').append('<tr data-sessionID=' + sessID + '>'n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;"> ' + sessTitle + '</span></td>'n<td width="100px" style="width:100px" class="tg-info" > ' + sessDate + '</td>'n<td width="100px" style="width:100px" class="tg-info"> ' + sessTime + '</td>'n<td width="50px" style="width:50px" class="tg-sessioncode">' + sessCode + '</td>'n</tr>'n');
        } else {
          sessDateVar.each(function( i, val ){
            $('#session-table-export').append('<tr data-sessionID=' + sessID + '>'n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;">' + sessTitle + '</span></td>'n<td width="100px" style="width:100px" class="tg-info" > ' + sessDates[i] + '</td>'n<td width="100px" style="width:100px" class="tg-info">' + sessTimes[i] + '</td>'n<td  width="50px" style="width:50px" class="tg-sessioncode"><p>' + sessCodes[i] + '</p></td></tr>'n');      
          });
        }
      });
      loadedFromCookie = true;
    } else if (askToLoad === false) {
      Cookies.remove('session-table', {
        path: '/isotope'
      });
      Cookies.remove('session-table-export', {
        path: '/isotope'
      });
      $('#table-container').append('<table class="tg" id="session-table">'n<thead>'n<tr>'n<th class="tg-yw41">Session</th>'n<th class="tg-yw4l">Date</th>'n<th class="tg-yw4l">Time</th>'n<th class="tg-yw4l">'n</th>'n</tr>'n</thead>'n<tbody>'n</tbody>'n</table>');
      $('.table-container-export').append('<table class="tg" cellpadding="10" id="session-table-export">'n<thead>'n<tr>'n<th class="tg-title">Session</th>'n<th class="tg-info">Date</th>'n<th class="tg-info">Time</th>'n<th class="tg-info">Session Code</th>'n</tr>'n</thead>'n<tbody>'n</tbody>'n</table>');
    }
  }
};
writeTable();

有问题的循环

        if (sessDates === false) {
          $('#session-table-export').append('<tr data-sessionID=' + sessID + '>'n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;"> ' + sessTitle + '</span></td>'n<td width="100px" style="width:100px" class="tg-info" > ' + sessDate + '</td>'n<td width="100px" style="width:100px" class="tg-info"> ' + sessTime + '</td>'n<td width="50px" style="width:50px" class="tg-sessioncode">' + sessCode + '</td>'n</tr>'n');
        } else {
          sessDateVar.each(function( i, val ){
            $('#session-table-export').append('<tr data-sessionID=' + sessID + '>'n<td width="210px" style="width:210px" class="tg-title" ><span style="font-size: 12px; font-weight: bold;">' + sessTitle + '</span></td>'n<td width="100px" style="width:100px" class="tg-info" > ' + sessDates[i] + '</td>'n<td width="100px" style="width:100px" class="tg-info">' + sessTimes[i] + '</td>'n<td  width="50px" style="width:50px" class="tg-sessioncode"><p>' + sessCodes[i] + '</p></td></tr>'n');      
            console.log('Wrote a row for session ' + sessCodes[i] + ' on date ' + sessDates[i] + '');
            console.log(i);
          });
        }

问题解决了!这段代码是较大文件的一部分,该文件执行影响此代码的其他操作。我无法粘贴整个文件,因此我粘贴到相关的部分,并尝试解释可能影响它的其他部分。

长话短说,tableDataExportParsed包含重复的 ID(当它将 IDS 存储在 cookie 中时,它会看到多个日期并为每个日期包含一个 ID),所以循环和代码非常好,它确实有一个重复的循环再次循环。因此,解决方案是清理tableDataExportParsed因此它不包括重复项。显然,在昨天写完这一切之后,我的大脑是朋友,并且有这种疏忽。