|
| 1 | +const MS_IN_A_SECOND = 1000 |
| 2 | +const MS_IN_A_MINUTE = MS_IN_A_SECOND * 60 |
| 3 | +const MS_IN_AN_HOUR = MS_IN_A_MINUTE * 60 |
| 4 | +const MS_IN_A_DAY = MS_IN_AN_HOUR * 24 |
| 5 | +const MS_IN_A_YEAR = MS_IN_A_DAY * 365 |
| 6 | + |
| 7 | +const meettingTime = Date.parse("2024-06-22T23:39:00.000Z") |
| 8 | +const birthdayTime = +new Date("2024-11-20") |
| 9 | + |
| 10 | +function getDiffTime({ start, end }) { |
| 11 | + const diff = end - start |
| 12 | + let rest = diff % MS_IN_A_YEAR |
| 13 | + |
| 14 | + const years = Math.floor(diff / MS_IN_A_YEAR) |
| 15 | + const days = Math.floor(rest / MS_IN_A_DAY) |
| 16 | + rest %= MS_IN_A_DAY |
| 17 | + const hours = Math.floor(rest / MS_IN_AN_HOUR) |
| 18 | + rest %= MS_IN_AN_HOUR |
| 19 | + const minutes = Math.floor(rest / MS_IN_A_MINUTE) |
| 20 | + rest %= MS_IN_A_MINUTE |
| 21 | + const seconds = Math.floor(rest / MS_IN_A_SECOND) |
| 22 | + |
| 23 | + return { years, days, hours, minutes, seconds } |
| 24 | +} |
| 25 | + |
| 26 | +function updateTimerElements(elements, { years, days, hours, minutes, seconds }) { |
| 27 | + elements.forEach(e => { |
| 28 | + const parentId = e.parentNode.id |
| 29 | + switch (parentId) { |
| 30 | + case 'year': |
| 31 | + if (e.textContent !== years.toString()) e.textContent = years |
| 32 | + break |
| 33 | + case 'day': |
| 34 | + if (e.textContent !== days.toString()) e.textContent = days |
| 35 | + break |
| 36 | + case 'hour': |
| 37 | + if (e.textContent !== hours.toString()) e.textContent = hours |
| 38 | + break |
| 39 | + case 'minute': |
| 40 | + if (e.textContent !== minutes.toString()) e.textContent = minutes |
| 41 | + break |
| 42 | + case 'second': |
| 43 | + if (e.textContent !== seconds.toString()) e.textContent = seconds |
| 44 | + break |
| 45 | + } |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +function handleTimeElement(element) { |
| 50 | + const timeElements = Array.from(element.querySelectorAll('.item-number')) |
| 51 | + |
| 52 | + return (start, end) => { |
| 53 | + const { years, days, hours, minutes, seconds } = getDiffTime({ start, end }) |
| 54 | + updateTimerElements(timeElements, { years, days, hours, minutes, seconds }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +document.addEventListener('DOMContentLoaded', () => { |
| 59 | + const meettingFunction = handleTimeElement(document.getElementById('meetting')) |
| 60 | + const birthdayFunction = handleTimeElement(document.getElementById('birthday')) |
| 61 | + |
| 62 | + setInterval(() => { |
| 63 | + meettingFunction(meettingTime, +new Date()) |
| 64 | + birthdayFunction(+new Date(), birthdayTime) |
| 65 | + }, 1000) |
| 66 | + |
| 67 | + const hiddenButton = document.getElementById('hidden-button') |
| 68 | + const dialog = document.getElementById('x-dialog') |
| 69 | + hiddenButton.addEventListener('click', () => { |
| 70 | + dialog.showModal() |
| 71 | + }) |
| 72 | + |
| 73 | + const no = document.getElementById('dialog-No') |
| 74 | + no.addEventListener('click', (e) => { |
| 75 | + e.preventDefault() |
| 76 | + alert('再给你一次机会') |
| 77 | + }) |
| 78 | + |
| 79 | + const ok = document.getElementById('dialog-Ok') |
| 80 | + ok.addEventListener('click', () => { |
| 81 | + dialog.close() |
| 82 | + alert('这还差不多,哼') |
| 83 | + }) |
| 84 | +}) |
0 commit comments