index.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>🎓🍅♨️</title>
  7. <style>
  8. /* 重置样式 */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. body {
  15. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  16. sans-serif;
  17. background-color: #f5f5f5;
  18. color: #333;
  19. height: 100vh;
  20. overflow: hidden;
  21. }
  22. /* 主容器 */
  23. .container {
  24. width: 100%;
  25. height: 100vh;
  26. display: flex;
  27. flex-direction: column;
  28. padding: 20px;
  29. gap: 20px;
  30. }
  31. /* 标题 */
  32. .title {
  33. text-align: center;
  34. font-size: 2rem;
  35. margin-bottom: 10px;
  36. }
  37. /* 内容区域 */
  38. .content {
  39. flex: 1;
  40. display: flex;
  41. flex-direction: column;
  42. gap: 20px;
  43. }
  44. /* 总学习时长 */
  45. .study-time {
  46. background-color: #fff;
  47. border-radius: 8px;
  48. padding: 20px;
  49. text-align: center;
  50. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  51. }
  52. .study-time-display {
  53. font-size: 3rem;
  54. font-weight: bold;
  55. color: #4caf50;
  56. }
  57. /* 番茄钟和空闲钟容器 */
  58. .timer-container {
  59. flex: 1;
  60. display: flex;
  61. flex-direction: column;
  62. gap: 20px;
  63. }
  64. /* 番茄钟 */
  65. .pomodoro {
  66. background-color: #fff;
  67. border-radius: 8px;
  68. padding: 20px;
  69. flex: 2;
  70. display: flex;
  71. flex-direction: column;
  72. align-items: center;
  73. justify-content: center;
  74. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  75. }
  76. /* 空闲钟 */
  77. .idle {
  78. background-color: #fff;
  79. border-radius: 8px;
  80. padding: 20px;
  81. flex: 1;
  82. display: flex;
  83. flex-direction: column;
  84. align-items: center;
  85. justify-content: center;
  86. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  87. }
  88. /* 圆形进度条容器 */
  89. .circular-progress {
  90. position: relative;
  91. width: 200px;
  92. height: 200px;
  93. margin: 20px 0;
  94. }
  95. .circular-progress svg {
  96. width: 100%;
  97. height: 100%;
  98. transform: rotate(-90deg);
  99. }
  100. .circular-progress .background {
  101. fill: none;
  102. stroke: #e0e0e0;
  103. stroke-width: 10;
  104. }
  105. .circular-progress .progress {
  106. fill: none;
  107. stroke: #ff5722;
  108. stroke-width: 10;
  109. stroke-linecap: round;
  110. stroke-dasharray: 565.48;
  111. stroke-dashoffset: 565.48;
  112. transition: stroke-dashoffset 1s linear;
  113. }
  114. .circular-progress .time-display {
  115. position: absolute;
  116. top: 50%;
  117. left: 50%;
  118. transform: translate(-50%, -50%);
  119. font-size: 2rem;
  120. font-weight: bold;
  121. }
  122. /* 条形进度条 */
  123. .linear-progress {
  124. width: 100%;
  125. height: 20px;
  126. background-color: #e0e0e0;
  127. border-radius: 10px;
  128. margin: 20px 0;
  129. overflow: hidden;
  130. }
  131. .linear-progress .progress {
  132. height: 100%;
  133. background-color: #2196f3;
  134. width: 0%;
  135. transition: width 1s linear;
  136. }
  137. /* 时间输入 */
  138. .time-input {
  139. font-size: 1.5rem;
  140. border: none;
  141. background: transparent;
  142. text-align: center;
  143. width: 100px;
  144. font-weight: bold;
  145. transition: all 0.3s ease;
  146. }
  147. .time-input:focus {
  148. outline: none;
  149. background-color: #f0f0f0;
  150. border-radius: 4px;
  151. transform: scale(1.05);
  152. }
  153. .time-input:disabled {
  154. cursor: not-allowed;
  155. opacity: 0.7;
  156. }
  157. /* 控制按钮 */
  158. .controls {
  159. display: flex;
  160. gap: 10px;
  161. margin-top: 20px;
  162. }
  163. .btn {
  164. background: none;
  165. border: none;
  166. font-size: 1.5rem;
  167. cursor: pointer;
  168. padding: 10px;
  169. border-radius: 50%;
  170. transition: all 0.3s ease;
  171. }
  172. .btn:hover {
  173. background-color: #f0f0f0;
  174. transform: scale(1.1);
  175. }
  176. .btn:active {
  177. transform: scale(0.95);
  178. }
  179. .btn:disabled {
  180. opacity: 0.5;
  181. cursor: not-allowed;
  182. transform: none;
  183. }
  184. /* 响应式布局 */
  185. @media (min-width: 768px) {
  186. .content {
  187. flex-direction: row;
  188. transition: flex-direction 0.3s ease;
  189. }
  190. .study-time {
  191. flex: 1;
  192. transition: flex 0.3s ease;
  193. }
  194. .timer-container {
  195. flex: 2;
  196. flex-direction: row;
  197. transition: flex 0.3s ease, flex-direction 0.3s ease;
  198. }
  199. }
  200. @media (min-width: 1200px) {
  201. .container {
  202. display: grid;
  203. grid-template-columns: 2fr 1fr;
  204. grid-template-rows: auto 1fr 1fr;
  205. gap: 20px;
  206. transition: all 0.3s ease;
  207. }
  208. .title {
  209. grid-column: 1 / -1;
  210. }
  211. .pomodoro {
  212. grid-row: 2 / 4;
  213. grid-column: 1;
  214. transition: all 0.3s ease;
  215. }
  216. .study-time {
  217. grid-row: 2;
  218. grid-column: 2;
  219. transition: all 0.3s ease;
  220. }
  221. .idle {
  222. grid-row: 3;
  223. grid-column: 2;
  224. transition: all 0.3s ease;
  225. }
  226. .content {
  227. display: contents;
  228. }
  229. .timer-container {
  230. display: contents;
  231. }
  232. }
  233. </style>
  234. </head>
  235. <body>
  236. <div class="container">
  237. <h1 class="title">🎓🍅♨️</h1>
  238. <div class="content">
  239. <div class="study-time">
  240. <h2>🎓</h2>
  241. <div class="study-time-display">0:00</div>
  242. </div>
  243. <div class="timer-container">
  244. <div class="pomodoro">
  245. <h2>🍅</h2>
  246. <div class="circular-progress">
  247. <svg>
  248. <circle class="background" cx="100" cy="100" r="90"></circle>
  249. <circle class="progress" cx="100" cy="100" r="90"></circle>
  250. </svg>
  251. <div class="time-display">
  252. <input type="text" class="time-input" value="25:00" readonly />
  253. </div>
  254. </div>
  255. <div class="controls">
  256. <button class="btn start-btn">▶️</button>
  257. <button class="btn pause-btn" style="display: none">⏸️</button>
  258. <button class="btn stop-btn" style="display: none">⏹️</button>
  259. </div>
  260. </div>
  261. <div class="idle">
  262. <h2>♨️</h2>
  263. <div class="linear-progress">
  264. <div class="progress"></div>
  265. </div>
  266. <div class="time-display">
  267. <input type="text" class="time-input" value="5:00" readonly />
  268. </div>
  269. <div class="controls">
  270. <button class="btn start-btn">▶️</button>
  271. <button class="btn pause-btn" style="display: none">⏸️</button>
  272. <button class="btn stop-btn" style="display: none">⏹️</button>
  273. </div>
  274. </div>
  275. </div>
  276. </div>
  277. </div>
  278. <script>
  279. // 数据管理
  280. const dataManager = {
  281. // 初始化数据
  282. init() {
  283. const data = localStorage.getItem("timerData");
  284. if (data) {
  285. return JSON.parse(data);
  286. } else {
  287. return {
  288. totalStudyTime: 0,
  289. pomodoroTime: 1500, // 25 分钟
  290. idleTime: 300, // 5 分钟
  291. };
  292. }
  293. },
  294. // 保存数据
  295. save(data) {
  296. localStorage.setItem("timerData", JSON.stringify(data));
  297. },
  298. // 格式化时间(秒转 HH:MM)
  299. formatTime(seconds) {
  300. const hours = Math.floor(seconds / 3600);
  301. const minutes = Math.floor((seconds % 3600) / 60);
  302. return `${hours}:${minutes.toString().padStart(2, "0")}`;
  303. },
  304. // 格式化倒计时(秒转 MM:SS)
  305. formatCountdown(seconds) {
  306. const minutes = Math.floor(seconds / 60);
  307. const secs = seconds % 60;
  308. return `${minutes}:${secs.toString().padStart(2, "0")}`;
  309. },
  310. };
  311. // 番茄钟控制器
  312. class PomodoroTimer {
  313. constructor(element) {
  314. this.element = element;
  315. this.timeInput = element.querySelector(".time-input");
  316. this.startBtn = element.querySelector(".start-btn");
  317. this.pauseBtn = element.querySelector(".pause-btn");
  318. this.stopBtn = element.querySelector(".stop-btn");
  319. this.progressCircle = element.querySelector(
  320. ".circular-progress .progress"
  321. );
  322. this.circumference = 2 * Math.PI * 90; // 圆的周长
  323. this.data = dataManager.init();
  324. this.totalTime = this.data.pomodoroTime;
  325. this.remainingTime = this.totalTime;
  326. this.isRunning = false;
  327. this.interval = null;
  328. this.init();
  329. }
  330. init() {
  331. this.timeInput.value = dataManager.formatCountdown(
  332. this.remainingTime
  333. );
  334. this.updateProgress();
  335. // 事件监听
  336. this.startBtn.addEventListener("click", () => this.start());
  337. this.pauseBtn.addEventListener("click", () => this.pause());
  338. this.stopBtn.addEventListener("click", () => this.stop());
  339. // 允许编辑时间
  340. this.timeInput.addEventListener("blur", () => {
  341. if (!this.isRunning) {
  342. const timeParts = this.timeInput.value.split(":");
  343. if (timeParts.length === 2) {
  344. const minutes = parseInt(timeParts[0], 10);
  345. const seconds = parseInt(timeParts[1], 10);
  346. // 验证输入
  347. if (
  348. !isNaN(minutes) &&
  349. !isNaN(seconds) &&
  350. minutes >= 0 &&
  351. minutes <= 99 &&
  352. seconds >= 0 &&
  353. seconds <= 59
  354. ) {
  355. this.totalTime = minutes * 60 + seconds;
  356. this.remainingTime = this.totalTime;
  357. this.data.pomodoroTime = this.totalTime;
  358. dataManager.save(this.data);
  359. this.updateProgress();
  360. }
  361. }
  362. // 重置为有效格式
  363. this.timeInput.value = dataManager.formatCountdown(
  364. this.remainingTime
  365. );
  366. }
  367. });
  368. // 限制输入格式
  369. this.timeInput.addEventListener("input", (e) => {
  370. let value = e.target.value;
  371. // 只允许数字和冒号
  372. value = value.replace(/[^0-9:]/g, "");
  373. // 自动格式化
  374. if (value.length > 2 && value.indexOf(":") === -1) {
  375. value = value.slice(0, 2) + ":" + value.slice(2);
  376. }
  377. // 限制长度
  378. if (value.length > 5) {
  379. value = value.slice(0, 5);
  380. }
  381. e.target.value = value;
  382. });
  383. }
  384. start() {
  385. if (!this.isRunning) {
  386. this.isRunning = true;
  387. this.timeInput.readOnly = true;
  388. this.startBtn.style.display = "none";
  389. this.pauseBtn.style.display = "inline-block";
  390. this.stopBtn.style.display = "inline-block";
  391. this.interval = setInterval(() => {
  392. this.remainingTime--;
  393. this.timeInput.value = dataManager.formatCountdown(
  394. this.remainingTime
  395. );
  396. this.updateProgress();
  397. if (this.remainingTime <= 0) {
  398. this.complete();
  399. }
  400. }, 1000);
  401. }
  402. }
  403. pause() {
  404. if (this.isRunning) {
  405. this.isRunning = false;
  406. clearInterval(this.interval);
  407. this.startBtn.style.display = "inline-block";
  408. this.pauseBtn.style.display = "none";
  409. }
  410. }
  411. stop() {
  412. this.isRunning = false;
  413. clearInterval(this.interval);
  414. // 将已进行的时间加入总学习时长
  415. const elapsed = this.totalTime - this.remainingTime;
  416. this.data.totalStudyTime += elapsed;
  417. dataManager.save(this.data);
  418. studyTimeDisplay.textContent = dataManager.formatTime(
  419. this.data.totalStudyTime
  420. );
  421. // 重置计时器
  422. this.remainingTime = this.totalTime;
  423. this.timeInput.value = dataManager.formatCountdown(
  424. this.remainingTime
  425. );
  426. this.timeInput.readOnly = false;
  427. this.updateProgress();
  428. this.startBtn.style.display = "inline-block";
  429. this.pauseBtn.style.display = "none";
  430. this.stopBtn.style.display = "none";
  431. }
  432. complete() {
  433. this.isRunning = false;
  434. clearInterval(this.interval);
  435. // 将全部时间加入总学习时长
  436. this.data.totalStudyTime += this.totalTime;
  437. dataManager.save(this.data);
  438. studyTimeDisplay.textContent = dataManager.formatTime(
  439. this.data.totalStudyTime
  440. );
  441. // 重置计时器
  442. this.remainingTime = this.totalTime;
  443. this.timeInput.value = dataManager.formatCountdown(
  444. this.remainingTime
  445. );
  446. this.timeInput.readOnly = false;
  447. this.updateProgress();
  448. this.startBtn.style.display = "inline-block";
  449. this.pauseBtn.style.display = "none";
  450. this.stopBtn.style.display = "none";
  451. // 发送通知
  452. this.sendNotification("番茄钟完成", "恭喜你完成了一个番茄钟!");
  453. }
  454. updateProgress() {
  455. const progress =
  456. (this.totalTime - this.remainingTime) / this.totalTime;
  457. const offset = this.circumference - progress * this.circumference;
  458. this.progressCircle.style.strokeDashoffset = offset;
  459. }
  460. sendNotification(title, message) {
  461. if ("Notification" in window) {
  462. if (Notification.permission === "granted") {
  463. new Notification(title, { body: message });
  464. } else if (Notification.permission !== "denied") {
  465. Notification.requestPermission().then((permission) => {
  466. if (permission === "granted") {
  467. new Notification(title, { body: message });
  468. }
  469. });
  470. }
  471. }
  472. // 播放声音
  473. const audio = new Audio(
  474. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  475. );
  476. audio.play();
  477. }
  478. }
  479. // 空闲钟控制器
  480. class IdleTimer {
  481. constructor(element) {
  482. this.element = element;
  483. this.timeInput = element.querySelector(".time-input");
  484. this.startBtn = element.querySelector(".start-btn");
  485. this.pauseBtn = element.querySelector(".pause-btn");
  486. this.stopBtn = element.querySelector(".stop-btn");
  487. this.progressBar = element.querySelector(
  488. ".linear-progress .progress"
  489. );
  490. this.data = dataManager.init();
  491. this.totalTime = this.data.idleTime;
  492. this.remainingTime = this.totalTime;
  493. this.isRunning = false;
  494. this.interval = null;
  495. this.init();
  496. }
  497. init() {
  498. this.timeInput.value = dataManager.formatCountdown(
  499. this.remainingTime
  500. );
  501. this.updateProgress();
  502. // 事件监听
  503. this.startBtn.addEventListener("click", () => this.start());
  504. this.pauseBtn.addEventListener("click", () => this.pause());
  505. this.stopBtn.addEventListener("click", () => this.stop());
  506. // 允许编辑时间
  507. this.timeInput.addEventListener("blur", () => {
  508. if (!this.isRunning) {
  509. const timeParts = this.timeInput.value.split(":");
  510. if (timeParts.length === 2) {
  511. const minutes = parseInt(timeParts[0], 10);
  512. const seconds = parseInt(timeParts[1], 10);
  513. // 验证输入
  514. if (
  515. !isNaN(minutes) &&
  516. !isNaN(seconds) &&
  517. minutes >= 0 &&
  518. minutes <= 99 &&
  519. seconds >= 0 &&
  520. seconds <= 59
  521. ) {
  522. this.totalTime = minutes * 60 + seconds;
  523. this.remainingTime = this.totalTime;
  524. this.data.idleTime = this.totalTime;
  525. dataManager.save(this.data);
  526. this.updateProgress();
  527. }
  528. }
  529. // 重置为有效格式
  530. this.timeInput.value = dataManager.formatCountdown(
  531. this.remainingTime
  532. );
  533. }
  534. });
  535. // 限制输入格式
  536. this.timeInput.addEventListener("input", (e) => {
  537. let value = e.target.value;
  538. // 只允许数字和冒号
  539. value = value.replace(/[^0-9:]/g, "");
  540. // 自动格式化
  541. if (value.length > 2 && value.indexOf(":") === -1) {
  542. value = value.slice(0, 2) + ":" + value.slice(2);
  543. }
  544. // 限制长度
  545. if (value.length > 5) {
  546. value = value.slice(0, 5);
  547. }
  548. e.target.value = value;
  549. });
  550. }
  551. start() {
  552. if (!this.isRunning) {
  553. this.isRunning = true;
  554. this.timeInput.readOnly = true;
  555. this.startBtn.style.display = "none";
  556. this.pauseBtn.style.display = "inline-block";
  557. this.stopBtn.style.display = "inline-block";
  558. this.interval = setInterval(() => {
  559. this.remainingTime--;
  560. this.timeInput.value = dataManager.formatCountdown(
  561. this.remainingTime
  562. );
  563. this.updateProgress();
  564. if (this.remainingTime <= 0) {
  565. this.complete();
  566. }
  567. }, 1000);
  568. }
  569. }
  570. pause() {
  571. if (this.isRunning) {
  572. this.isRunning = false;
  573. clearInterval(this.interval);
  574. this.startBtn.style.display = "inline-block";
  575. this.pauseBtn.style.display = "none";
  576. }
  577. }
  578. stop() {
  579. this.isRunning = false;
  580. clearInterval(this.interval);
  581. // 重置计时器
  582. this.remainingTime = this.totalTime;
  583. this.timeInput.value = dataManager.formatCountdown(
  584. this.remainingTime
  585. );
  586. this.timeInput.readOnly = false;
  587. this.updateProgress();
  588. this.startBtn.style.display = "inline-block";
  589. this.pauseBtn.style.display = "none";
  590. this.stopBtn.style.display = "none";
  591. }
  592. complete() {
  593. this.isRunning = false;
  594. clearInterval(this.interval);
  595. // 重置计时器
  596. this.remainingTime = this.totalTime;
  597. this.timeInput.value = dataManager.formatCountdown(
  598. this.remainingTime
  599. );
  600. this.timeInput.readOnly = false;
  601. this.updateProgress();
  602. this.startBtn.style.display = "inline-block";
  603. this.pauseBtn.style.display = "none";
  604. this.stopBtn.style.display = "none";
  605. // 发送通知
  606. this.sendNotification("空闲时间结束", "休息时间结束了!");
  607. }
  608. updateProgress() {
  609. const progress =
  610. (this.totalTime - this.remainingTime) / this.totalTime;
  611. this.progressBar.style.width = `${progress * 100}%`;
  612. }
  613. sendNotification(title, message) {
  614. if ("Notification" in window) {
  615. if (Notification.permission === "granted") {
  616. new Notification(title, { body: message });
  617. } else if (Notification.permission !== "denied") {
  618. Notification.requestPermission().then((permission) => {
  619. if (permission === "granted") {
  620. new Notification(title, { body: message });
  621. }
  622. });
  623. }
  624. }
  625. // 播放声音
  626. const audio = new Audio(
  627. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  628. );
  629. audio.play();
  630. }
  631. }
  632. // 初始化应用
  633. const data = dataManager.init();
  634. const studyTimeDisplay = document.querySelector(".study-time-display");
  635. studyTimeDisplay.textContent = dataManager.formatTime(
  636. data.totalStudyTime
  637. );
  638. // 初始化番茄钟
  639. const pomodoroElement = document.querySelector(".pomodoro");
  640. const pomodoroTimer = new PomodoroTimer(pomodoroElement);
  641. // 初始化空闲钟
  642. const idleElement = document.querySelector(".idle");
  643. const idleTimer = new IdleTimer(idleElement);
  644. // 请求通知权限
  645. if ("Notification" in window && Notification.permission === "default") {
  646. Notification.requestPermission();
  647. }
  648. // 处理页面可见性变化
  649. let lastVisibleTime = Date.now();
  650. document.addEventListener("visibilitychange", () => {
  651. if (document.visibilityState === "visible") {
  652. // 页面变为可见时,计算经过的时间并更新计时器
  653. const now = Date.now();
  654. const elapsedSeconds = Math.floor((now - lastVisibleTime) / 1000);
  655. if (pomodoroTimer.isRunning) {
  656. pomodoroTimer.remainingTime = Math.max(
  657. 0,
  658. pomodoroTimer.remainingTime - elapsedSeconds
  659. );
  660. pomodoroTimer.timeInput.value = dataManager.formatCountdown(
  661. pomodoroTimer.remainingTime
  662. );
  663. pomodoroTimer.updateProgress();
  664. if (pomodoroTimer.remainingTime <= 0) {
  665. pomodoroTimer.complete();
  666. }
  667. }
  668. if (idleTimer.isRunning) {
  669. idleTimer.remainingTime = Math.max(
  670. 0,
  671. idleTimer.remainingTime - elapsedSeconds
  672. );
  673. idleTimer.timeInput.value = dataManager.formatCountdown(
  674. idleTimer.remainingTime
  675. );
  676. idleTimer.updateProgress();
  677. if (idleTimer.remainingTime <= 0) {
  678. idleTimer.complete();
  679. }
  680. }
  681. }
  682. lastVisibleTime = Date.now();
  683. });
  684. // 页面关闭前提醒
  685. window.addEventListener("beforeunload", (e) => {
  686. if (pomodoroTimer.isRunning || idleTimer.isRunning) {
  687. e.preventDefault();
  688. e.returnValue = "您有正在运行的计时器,确定要离开吗?";
  689. return e.returnValue;
  690. }
  691. });
  692. </script>
  693. </body>
  694. </html>