2016-03-16 114 views
0

下面的代碼應該從一個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); 
      }); 

     } 
+0

您是否在循環前嘗試驗證sessDateVar內容? –

+0

我做了,並且實際上我只是使用.each()作爲while循環的替代方法(以便它只會循環多次,因爲有附加到該會話的日期)。但即使我用while循環替換.each(),我也遇到了同樣的問題。 –

+0

你能告訴我sessDateVar –

回答

0

問題解決了!這段代碼是影響此代碼的其他內容的較大文件的一部分。我無法粘貼我的整個文件,所以我粘貼到相關的部分,並試圖解釋可能影響它的其他部分。

長話短說,tableDataExportParsed被包括重複的ID(當它存儲在cookie中它看到多個日期,並列入各一個ID的IDS),所以循環和代碼是完全正常的,這是有確實是一個複製它再次循環。因此解決方案是清除tableDataExportParsed,因此它不包含重複項。很明顯,我的大腦昨天寫完所有內容併成爲了朋友,並進行了監督。