Find the Bugs چالش 6 از جلسهی 27، سخت
این ثانیهشمار روی 0 میماند. دو اشتباه دارد:
- تمیزکاری درست نوشته نشده و تایمر همان اول خاموش میشود.
- حتی اگر تایمر کار کند، عدد از 1 بالاتر نمیرود.
هر دو را درست کن تا هر ثانیه یکی زیاد شود.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<div id="root" dir="ltr"></div>
<script type="text/jsx">
import { useState, useEffect } from "react";
import { createRoot } from "react-dom/client";
function App() {
const [seconds, setSeconds] = useState(0);
useEffect(() => {
const timer = setInterval(() => {
setSeconds(seconds + 1);
}, 1000);
return clearInterval(timer);
}, []);
return <h1 id="sec">{seconds}</h1>;
}
createRoot(document.getElementById("root")).render(<App />);
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 27: تایمر و تمیزکاری
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js