1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
為了解決上面的問題,像這樣編寫CSS類
CSS:
.time-picker{
border:2px solid white;
}
.selected{
border: 2px solid green;
}
在js中,當(dāng)類更改時(shí),使用localstorage來(lái)存儲(chǔ)timepicker的id和內(nèi)容。
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 關(guān)注
- 148 瀏覽
添加回答
舉報(bào)