"use client"; import { useState, useEffect, useRef } from "react"; import { Hourglass, Play, Pause, RotateCcw, BellRing } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; export default function CountdownPage() { const [inputMinutes, setInputMinutes] = useState("5"); const [timeLeft, setTimeLeft] = useState(300); // 5 minutes in seconds const [running, setRunning] = useState(false); const [finished, setFinished] = useState(false); const timerRef = useRef(null); useEffect(() => { if (running && timeLeft > 0) { timerRef.current = setInterval(() => { setTimeLeft((prev) => prev - 1); }, 1000); } else if (timeLeft === 0 && running) { setRunning(false); setFinished(true); toast.success("时间到!", { icon: , duration: 5000 }); // Try to play a subtle beep if browser allowed try { const audioCtx = new (window.AudioContext || (window as any).webkitAudioContext)(); const oscillator = audioCtx.createOscillator(); oscillator.type = 'sine'; oscillator.frequency.setValueAtTime(440, audioCtx.currentTime); oscillator.connect(audioCtx.destination); oscillator.start(); oscillator.stop(audioCtx.currentTime + 0.5); } catch {} } else { clearInterval(timerRef.current); } return () => clearInterval(timerRef.current); }, [running, timeLeft]); const startTimer = () => { if (timeLeft === 0) { const secs = parseInt(inputMinutes) * 60; if (isNaN(secs) || secs <= 0) { toast.error("请输入有效的分钟数"); return; } setTimeLeft(secs); } setRunning(true); setFinished(false); }; const handleReset = () => { setRunning(false); setFinished(false); const secs = parseInt(inputMinutes) * 60 || 300; setTimeLeft(secs); }; const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60); const secs = seconds % 60; return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`; }; const progress = timeLeft / (parseInt(inputMinutes) * 60 || 300); return (

倒数计时器

设置并开始倒计时提醒

{/* Circular Progress SVG */}
{formatTime(timeLeft)}
分钟 { setInputMinutes(e.target.value); if (!running) setTimeLeft(parseInt(e.target.value) * 60 || 0); }} disabled={running} className="text-center font-bold text-lg" />
{!running ? ( ) : ( )}
{finished && (

时间到!

)}
); }