Javascript 将时间戳保存在“localStorage”中并保留这些标记

Javascript 将时间戳保存在“localStorage”中并保留这些标记,javascript,html,local-storage,Javascript,Html,Local Storage,我有一份分数申请表。 工作原理如下:每次点击按钮我都会保存一个时间戳, 你只能拨四次,这已经完成了。 我需要将四个时间戳保存在localStorage中,并保持这些标记不过期,然后在前几天将新标记制作并显示在前一天标记的下方 函数getTime{ document.querySelectorbutton.setAttributeonclick,弹出警报; var storedNames=JSON.parselocalStorage.getItemnames; var数据=新日期; var fu

我有一份分数申请表。 工作原理如下:每次点击按钮我都会保存一个时间戳, 你只能拨四次,这已经完成了。 我需要将四个时间戳保存在localStorage中,并保持这些标记不过期,然后在前几天将新标记制作并显示在前一天标记的下方

函数getTime{ document.querySelectorbutton.setAttributeonclick,弹出警报; var storedNames=JSON.parselocalStorage.getItemnames; var数据=新日期; var full_time=data.getHours+:+data.getMinutes+:+ data.getSeconds; var completeDate=全职; setItem'completeDate',completeDate; document.getElementByIdmarkTimes.insertAdjacentHTML'beforeend', +完全态+“”; var qtd=document.getElementsByClassNamedataTime.length; 如果qtd<4{ setTimeoutfunction{ document.querySelectorbutton.setAttributeonclick,getTime; }, 3000; }否则{ document.querySelectorbutton.disabled=true; console.logfim; } } 马卡尔
您需要存储一个数组:

所以你的台词

localStorage.setItem('completeDate', completeDate);
请执行以下操作:

const savedDates = localStorage.getItem('completeDate')

let dates
if (savedDates) {
  dates = JSON.parse(savedDates)

  dates.push(completeDate)
} else {
  dates = [completeDate]
}

localStorage.setItem('completeDate', JSON.stringify(dates))
它将在数组中添加项并将其保存回去。
不幸的是,您需要为localStorage解析、修改和字符串化数据

havenchyk,我得到一个错误:位置2 dates=JSON的JSON中的意外标记。parsesavedDates@josecarlos尝试在localstorage中清除该键,可能保存了旧值