screen_rotation
Copied to Clipboard
<html> <head> <meta charset="UTF-8" /> <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.development.js"></script> <script src="https://unpkg.com/@babel/standalone@7.14.7/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect, useRef } = React; function App() { const [message, setMessage] = useState(""); const intervalIdRef = useRef(null); useEffect(() => { intervalIdRef.current = setInterval(() => { setMessage(new Date().toLocaleTimeString()); }, 1000); return () => clearInterval(intervalIdRef.current); }, []); return ( <div class='container'> <h3>React clearInterval Method | Stop interval</h3> <p>{message}</p> <button onClick={() => clearInterval(intervalIdRef.current)}> Stop </button> </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> .container { width: 100%; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f0f0f0; text-align: center; } h3 { font-size: 24px; margin-bottom: 10px; } p { font-size: 16px; margin-bottom: 20px; } button { padding: 10px 20px; font-size: 16px; background-color: #007bff; color: #fff; border: none; border-radius: 4px; cursor: pointer; } button:hover { background-color: #0056b3; } </style> </body> </html>