Function: seven days for a cycle check-in function.

Ideas:

  • Store the check-in time as an array
  • Determine whether the date has been signed

1. Signed in: directly use the stored array to judge the activation status. 2. Unsigned in: (1) No sign in record: empty array

  • Compares the number of days apart with the remaining positions in the array

Less than: indicates the empty value of the days separated from each other and the date of the current day. 2. Greater than or equal to: indicates the start of the next cycle

The test data

// Let signArr=[] let signIn=false // Let signArr=["2021-11-21",false,"2021-11-23"] let signIn=false // Let signIn=false Let signArr=["2021-11-21","2021-11-22"] let signArr= true // Let signArr=['2021-11-15', '2021-11-16'] let signIn=false let signArr = ['2021-10-1', false, '2021-10-3'] let signIn = falseCopy the code

The specific code is as follows

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta  name="viewport" content="width=device-width, Initial-scale =1.0"> <title>Document</title> </head> <body> <ul></ul> <button> signArr = ['2021-11-20', false, '2021-11-22'] let signIn = false function getIntervalDay(a, b) { if (! a || ! b) return 0 a = a.split('-').map(item => +item) b = b.split('-').map(item => +item) const currentTime = new Date(a[0], a[1] - 1, a[2]).getTime() const lastTime = new Date(b[0], b[1] - 1, b[2]).getTime() return (currentTime - lastTime) / 1000 / 60 / 60 / 24 - 1 } function getActiveArr(signArr, signIn) { if (! signIn && signArr.length) { const currentDate = new Date() const intervalDay = getIntervalDay( `${currentDate.getFullYear()}-${currentDate.getMonth() + 1}-${currentDate.getDate()}`, Console. log(' ${currentDate.getFullYear()}-${currentDate.getMonth() + 1}-${currentDate.getDate()}`) if (intervalDay < 7 - signArr.length) { return signArr } else { return [] } } else { Function render(ActiveArr) {ul.innerhtml = "for (let I = 0; i < 7; i++) { const li = document.createElement('li') li.innerText = ActiveArr[i] ul.appendChild(li) } } const ul = document.querySelector('ul') const btn = document.querySelector('button') let ActiveArr = getActiveArr(signArr, Onclick = function () {if (signIn) return alert(' already signed ') const currentDate = new Date() const currentDay = `${currentDate.getFullYear()}-${currentDate.getMonth() + 1}-${currentDate.getDate()}` const intervalDay = getIntervalDay( currentDay, ActiveArr[ActiveArr.length - 1] ) if (intervalDay < 7 - ActiveArr.length) { ActiveArr.push(... new Array(intervalDay).fill(false)) } else { ActiveArr = [] } ActiveArr.push(currentDay) signArr = ActiveArr signIn = true render(ActiveArr) } </script> </body> </html>Copy the code