Search

_0504_setTimeout으로interval구현

대분류
STEP05 비동기
소분류
비동기

영상

개념

<!DOCTYPE html> <html lang="ko"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <button onclick="startCount()">시작</button> <input type="text" id="number"> <button onclick="stopCount()">중지</button> <script> let id = 0; let cnt = 0; function startCount() { setCount(); } function setCount() { document.getElementById("number").value = cnt; cnt++; id = setTimeout(setCount, 1000); } function stopCount() { clearTimeout(id); } </script> </body> </html>
Java
복사