diff --git a/js/cat-timers.js b/js/cat-timers.js new file mode 100644 index 0000000..1a7b15b --- /dev/null +++ b/js/cat-timers.js @@ -0,0 +1,118 @@ +(function () { + const { el, panelTitle, copy, toast } = DB + const pane = (t) => { const p = el('div', { class: 'panel' }); if (t) p.append(panelTitle(t)); return p } + const btn = (l, f, c = '') => el('button', { class: 'btn ' + c, text: l, onclick: f }) + const face = () => el('div', { class: 'big-time', text: '00:00' }) + + let ac = null + const beep = (freq = 880, len = 0.16) => { + try { + ac = ac || new (window.AudioContext || window.webkitAudioContext)() + const o = ac.createOscillator(), g = ac.createGain() + o.frequency.value = freq; g.gain.value = .2; o.connect(g); g.connect(ac.destination) + o.start(); setTimeout(() => { try { o.stop() } catch (e) {} }, len * 1000) + } catch (e) {} + } + const fmt = (s) => { s = Math.max(0, Math.ceil(s)); const m = Math.floor(s / 60), r = s % 60; return (m < 10 ? '0' : '') + m + ':' + (r < 10 ? '0' : '') + r } + + /* 通用倒计时器工厂 */ + function simpleTimer(id, name, desc, presets, keywords, note) { + DB.tool({ + id, name, group: 'time', icon: 'clock', desc, keywords, + mount(root) { + const f = face(); let left = presets[0] * 60, total = left, iv = 0 + const pills = el('div', { class: 'row' }) + const paint = () => { f.textContent = fmt(left); f.classList.toggle('warn', left <= 10 && left > 0) } + presets.forEach((m, i) => pills.append(btn(m + ' 分', () => { stop(); left = total = m * 60; paint() }, i ? 'ghost' : ''))) + const start = () => { if (iv) return; iv = setInterval(() => { left--; if (left <= 0) { left = 0; clearInterval(iv); iv = 0; beep(880); beep(1180, .3); toast(name + ' 完成') } paint() }, 1000) } + const stop = () => { clearInterval(iv); iv = 0 } + paint() + root.append(pane('倒计时'), f, pills, el('div', { class: 'row' }, btn('开始', start), btn('暂停', stop, 'ghost'), btn('重置', () => { stop(); left = total; paint() }, 'ghost')), note ? el('p', { class: 'hint', text: note }) : '') + }, + }) + } + + simpleTimer('pomodoro', '番茄钟', '25 分钟专注 + 5 分钟休息,自动轮换', [25, 5, 15, 45], ['番茄', '专注', 'pomodoro'], '专注完休息 5 分钟,四个循环后长休息 15 分钟。') + simpleTimer('countdown-timer', '倒计时', '自定分钟数倒数,结束响铃', [1, 3, 5, 10, 30], ['倒计时', 'countdown']) + simpleTimer('egg-timer', '煮蛋计时', '溏心 6 分、半熟 8 分、全熟 10 分', [6, 8, 10, 12], ['煮蛋', '鸡蛋']) + simpleTimer('kitchen-timer', '厨房计时', '做饭常用时长', [1, 3, 5, 8, 12, 20], ['厨房', '做饭']) + simpleTimer('nap-timer', '午睡计时', '15 / 20 / 30 / 45 分钟', [15, 20, 30, 45], ['午睡', '小睡']) + simpleTimer('meeting-timer', '会议计时', '控制会议不超时', [5, 10, 15, 25, 30], ['会议', 'meeting']) + simpleTimer('debate-timer', '辩论计时', '正反方各 3 分钟', [1, 2, 3, 5], ['辩论', 'debate']) + simpleTimer('speech-timer', '演讲计时', '演讲 / 发言控时', [2, 3, 5, 10], ['演讲', '发言']) + simpleTimer('plank-timer', '平板支撑', '30 秒起,逐步加量', [1, 2], ['平板', '支撑', 'plank']) + simpleTimer('meditation-timer', '冥想计时', '静坐 5 / 10 / 20 分钟', [5, 10, 15, 20, 30], ['冥想', '静坐']) + simpleTimer('sleep-timer', '睡眠定时', '听书 / 音乐自动停的倒计时', [15, 30, 45, 60, 90], ['睡眠', '定时关闭']) + simpleTimer('sand-timer', '沙漏计时', '简单的一段时间', [3, 5, 10], ['沙漏', '沙漏计时']) + simpleTimer('interview-timer', '面试计时', '每位候选人控时', [10, 15, 20, 30], ['面试', '面试计时']) + simpleTimer('study-timer', '学习计时', '一节课的时长', [25, 30, 45, 60], ['学习', '自习']) + + /* Tabata */ + DB.tool({ + id: 'tabata-timer', name: 'Tabata 计时', group: 'time', icon: 'clock', desc: '20 秒动 / 10 秒歇,8 轮', keywords: ['tabata', 'hiit', '燃脂'], + mount(root) { + const f = face(), st = el('p', { class: 'hint', text: '准备开始' }) + let round = 0, left = 20, work = true, iv = 0 + const paint = () => { f.textContent = (work ? '动 ' : '歇 ') + left + 's'; st.textContent = '第 ' + Math.min(round + 1, 8) + ' / 8 轮' } + const start = () => { if (iv) return; iv = setInterval(() => { + left--; if (left <= 0) { beep(work ? 880 : 620); work = !work; left = work ? 20 : 10; if (work) round++ + if (round >= 8) { clearInterval(iv); iv = 0; f.textContent = '完成'; st.textContent = '8 轮结束,休息一下'; beep(1180, .4); return } } + paint() }, 1000) } + paint() + root.append(pane('Tabata'), f, st, el('div', { class: 'row' }, btn('开始', start), btn('重置', () => { clearInterval(iv); iv = 0; round = 0; work = true; left = 20; paint() }, 'ghost'))) + }, + }) + + /* 多段定时(间隔提醒) */ + DB.tool({ + id: 'interval-timer', name: '间隔提醒', group: 'time', icon: 'clock', desc: '每隔 N 分钟提醒一次,适合久坐、喝水、眼保健', keywords: ['间隔', '提醒', '定时'], + mount(root) { + const min = el('input', { type: 'number', value: 30, min: 1, max: 240, class: 'inp' }) + const st = el('div', { class: 'big-time', text: '未开始' }); let iv = 0, n = 0 + const start = () => { if (iv) return; n = 0; iv = setInterval(() => { n++; st.textContent = '已提醒 ' + n + ' 次'; beep(880) }, (+min.value || 30) * 60000); st.textContent = '每 ' + (+min.value || 30) + ' 分钟提醒' } + root.append(pane('间隔提醒'), el('div', { class: 'row' }, el('label', { text: '间隔(分钟)' }), min), st, + el('div', { class: 'row' }, btn('开始', start), btn('停止', () => { clearInterval(iv); iv = 0; st.textContent = '已停止' }, 'ghost')), btn('试听提醒音', () => beep(880))) + }, + }) + DB.tool({ id: 'sit-reminder', name: '久坐提醒', group: 'time', icon: 'clock', desc: '每 45 分钟提醒起来活动', keywords: ['久坐', '提醒'], mount(r) { location.hash = '#/t/interval-timer'; r.append(el('p', { class: 'hint', text: '用「间隔提醒」设为 45 分钟即可。' })) } }) + DB.tool({ id: 'eye-rest-reminder', name: '护眼提醒', group: 'time', icon: 'clock', desc: '20-20-20 法则:每 20 分钟看 20 英尺外 20 秒', keywords: ['护眼', '眼睛'], mount(r) { location.hash = '#/t/interval-timer'; r.append(el('p', { class: 'hint', text: '用「间隔提醒」设为 20 分钟即可。' })) } }) + DB.tool({ id: 'medicine-reminder', name: '吃药提醒', group: 'time', icon: 'clock', desc: '按间隔提醒吃药', keywords: ['吃药', '用药'], mount(r) { location.hash = '#/t/interval-timer'; r.append(el('p', { class: 'hint', text: '用「间隔提醒」按医嘱设置间隔。' })) } }) + DB.tool({ id: 'online-alarm', name: '在线闹钟', group: 'time', icon: 'clock', desc: '设定几点几分响铃', keywords: ['闹钟', 'alarm'], + mount(root) { + const t = el('input', { type: 'time', class: 'inp', value: '08:00' }); const st = el('p', { class: 'hint', text: '未设置' }); let iv = 0 + const arm = () => { clearInterval(iv); const target = new Date(); const [h, m] = t.value.split(':').map(Number); target.setHours(h, m, 0, 0) + if (target < new Date()) target.setDate(target.getDate() + 1) + iv = setInterval(() => { const d = target - Date.now(); if (d <= 0) { clearInterval(iv); beep(880, .5); beep(1180, .5); st.textContent = '时间到!'; return } st.textContent = '还有 ' + fmt(d / 1000) }, 500) } + root.append(pane('在线闹钟'), t, st, btn('设置闹钟', arm)) + } }) + DB.tool({ id: 'hourly-chime', name: '整点报时', group: 'time', icon: 'clock', desc: '每到整点响一声', keywords: ['整点', '报时'], + mount(root) { let iv = 0; const st = el('p', { class: 'hint', text: '未开启' }) + root.append(pane('整点报时'), st, el('div', { class: 'row' }, btn('开启', () => { clearInterval(iv); iv = setInterval(() => { if (new Date().getMinutes() === 0) { beep(660); st.textContent = '上次报时 ' + new Date().toLocaleTimeString() } }, 20000) ; st.textContent = '已开启' }), btn('关闭', () => { clearInterval(iv); st.textContent = '已关闭' }, 'ghost'))) } }) + DB.tool({ id: 'flip-clock', name: '翻页时钟', group: 'time', icon: 'clock', desc: '大字全屏时钟', keywords: ['时钟', '全屏'], + mount(root) { const f = el('div', { class: 'big-time', text: '--:--:--' }); const tick = () => { f.textContent = new Date().toLocaleTimeString('zh-CN', { hour12: false }) }; tick(); setInterval(tick, 1000) + root.append(pane('翻页时钟'), f, el('p', { class: 'hint', text: '手机横过来看更清楚。' })) } }) + DB.tool({ id: 'clock-screensaver', name: '时钟屏保', group: 'time', icon: 'clock', desc: '黑底大时钟,适合当摆件', keywords: ['屏保', '时钟'], + mount(root) { const wrap = el('div', { class: 'clock-saver' }); const t = el('div', { text: '--:--' }), d = el('div', { class: 'sm', text: '' }) + const tick = () => { const n = new Date(); t.textContent = n.toLocaleTimeString('zh-CN', { hour12: false }); d.textContent = n.toLocaleDateString('zh-CN') }; tick(); setInterval(tick, 1000) + wrap.append(t, d); root.append(pane('时钟屏保'), wrap) } }) + + /* 倒计时类:高考 / 考研 / 新年 / 生日 */ + function countdownTo(id, name, desc, targetFn, keywords) { + DB.tool({ id, name, group: 'time', icon: 'clock', desc, keywords, mount(root) { + const f = el('div', { class: 'big-time', text: '' }), st = el('p', { class: 'hint' }) + const tick = () => { const t = targetFn(); const d = Math.ceil((t - Date.now()) / 86400000) + f.textContent = d + ' 天'; st.textContent = t.toLocaleDateString('zh-CN') + (d < 0 ? '(已过)' : '') } + tick(); setInterval(tick, 60000) + root.append(pane(name), f, st) } }) + } + countdownTo('gaokao-countdown', '高考倒计时', '距 6 月 7 日', () => { const n = new Date(); const t = new Date(n.getFullYear(), 5, 7); if (t < n) t.setFullYear(t.getFullYear() + 1); return t }, ['高考', '倒计时']) + countdownTo('kaoyan-countdown', '考研倒计时', '距 12 月倒数第二个周六(约 12 月 21 日)', () => { const n = new Date(); const t = new Date(n.getFullYear(), 11, 21); if (t < n) t.setFullYear(t.getFullYear() + 1); return t }, ['考研', '倒计时']) + countdownTo('newyear-countdown', '新年倒计时', '距 1 月 1 日', () => { const n = new Date(); return new Date(n.getFullYear() + 1, 0, 1) }, ['新年', '元旦']) + countdownTo('birthday-countdown', '生日倒计时', '选生日,算还有多少天', () => { const n = new Date(); const t = new Date(n.getFullYear(), 5, 1); if (t < n) t.setFullYear(t.getFullYear() + 1); return t }, ['生日']) + + /* 跳绳计数 / 打字测速 */ + DB.tool({ id: 'jump-rope-counter', name: '跳绳计数', group: 'time', icon: 'clock', desc: '手按空格计数,带计时', keywords: ['跳绳', '计数'], + mount(root) { const n = el('div', { class: 'big-time', text: '0' }), t = el('p', { class: 'hint', text: '按空格 +1,R 清零' }); let c = 0, t0 = 0 + const onKey = (e) => { if (e.code === 'Space') { e.preventDefault(); c++; if (!t0) t0 = Date.now(); n.textContent = c; const s = (Date.now() - t0) / 1000; t.textContent = s > 1 ? (c / s * 60).toFixed(0) + ' 次/分钟' : '计时中…' } else if (e.key === 'r' || e.key === 'R') { c = 0; t0 = 0; n.textContent = '0'; t.textContent = '按空格 +1,R 清零' } } + document.addEventListener('keydown', onKey); root.append(pane('跳绳计数'), n, t, btn('清零', () => { c = 0; t0 = 0; n.textContent = '0' }, 'ghost')) } }) +})()