웹 프로그래밍 기초/BOM 브라우저 객체

setTimeout(), setInterval()

별초롱언니 2025. 4. 10. 16:33

window.setTimeout(function, milliseconds);

// function : 실행 할 함수

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <p>wait 3 seconds.</p>
  <button onclick="setTimeout(myFunction, 3000);">Try it</button>
  <script>
    function myFunction() {
      alert('Hello');
    }
  </script>
</body>
</html>


clearTimeout(), clearInterval()

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button onclick="myFunction()">Try it</button>
  <button onclick="myStopFunction()">Stop</button>

  <script>
    let myVar;
    function myFunction() {
      myVar = setTimeout(function(){alert("Hello");},3000);
    }
    function myStopFunction() {
      clearTimeout(myVar);
    }
  </script>
</body>
</html>

'웹 프로그래밍 기초 > BOM 브라우저 객체' 카테고리의 다른 글

location 객체  (0) 2025.04.10
screen 객체  (0) 2025.04.10
resizeTo(), resizeBy()  (0) 2025.04.10
moveTo(), moveBy()  (0) 2025.04.10
Browser Object Model  (1) 2025.04.10