screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect } = React; function App() { const [timeLeft, setTimeLeft] = useState(18000); // seconds (5 hours) useEffect(() => { const timer = setInterval(() => { setTimeLeft(prevTime => prevTime - 1); if (timeLeft === 0) { clearInterval(timer); // countdown finished } }, 1000); return () => { clearInterval(timer); }; }, []); const hours = Math.floor(timeLeft / 3600).toString().padStart(2, '0'); const minutes = Math.floor((timeLeft % 3600) / 60).toString().padStart(2, '0'); const seconds = (timeLeft % 60).toString().padStart(2, '0'); const handleReset = () => { setTimeLeft(18000); // Reset time to 5 hours }; return ( <div className='container'> <h2>React Js Countdown Timer</h2> <div className="timer"> <span className="time">{hours}</span> : <span className="time">{minutes}</span> : <span className="time">{seconds}</span> </div> <button onClick={handleReset}>Reset</button> </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> .container { text-align: center; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.12), 0 2px 4px 0 rgba(0, 0, 0, 0.24); width: 600px; margin: 0 auto; padding: 20px; } h2 { font-size: 24px; margin-bottom: 20px; } .timer { display: flex; align-items: center; justify-content: center; font-size: 36px; margin-bottom: 20px; } .time { padding: 5px; background-color: #333; color: #fff; border-radius: 4px; } button { padding: 10px 20px; background-color: #007bff; color: #fff; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease; } button:hover { background-color: #0056b3; } </style> </body> </html>