1 回答
TA贡献1865条经验 获得超7个赞
为了解决上面的问题,像这样编写CSS类
CSS:
.time-picker{
border:2px solid white;
}
.selected{
border: 2px solid green;
}
在js中,当类更改时,使用localstorage来存储timepicker的id和内容。
JS:
var timePickerList = document.querySelectorAll('.time-picker');
//check if alarms are there in localstorage otherwise return empty array
var alarms=localStorage.getItem('alarms')? JSON.parse(localStorage.getItem('alarms')):[];
timePickerArray.forEach(function(elem) {
if(alarms.find( alarm => elem.id == alarm.id)){
elem.classList.add('selected'); //if alarm contains ele id,set background green
}
elem.addEventListener("click", function() {
elem.classList.toggle('selected');//
if(elem.classList.contains('selected'))
{
alarms.push({id:elem.id,content:elem.innerHTML});//
localStorage.setItem('alarms',JSON.stringify(alarms));
}
else{
let index=alarms.findIndex( alarm => alarm.id=elem.id)
if(index!=-1){
alarms.splice(index,1);//Remove from alarms array if alarm is disabled
localStorage.setItem('alarms',JSON.stringify(alarms));
}
}
});
})
function playAlarm(){
let currentTimeForAlarm = new Date().toLocaleTimeString('en-GB', { hour: "numeric", minute: "numeric"});
let currentAlarm=alarms.find( alarm => alarm.content == currentTimeForAlarm);
if(currentAlarm){
console.log(`Alarm from ${alarm.id} and content is ${alarm.content}`)
sound();
}
}
setInterval(playAlarm, 1000);
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报