Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

일단 적는

setTimeout() 본문

javascript

setTimeout()

Hoon.Wi 2023. 1. 13. 03:44

일정 시간 이후 동작을 수행하는 setTimeout() 함수

 

setTimeout(할일, 시간)

시간 = 1초 = 1000
할일 = function(){실제로 할 일}

실제로 할 일 = alert('성공')

시간 -> 할일 -> 수행할 동작 순서대로 작성하면 에러가 날 확률이 적다.

// 일정시간 이후 실행 setTimeout(function(){}, time)
setTimeout(function(){alert('성공')}, 3000)

// 멈추기 clearTimeout(function(){}, time)
const timer = setTimeout(function(){alert('뜨기 전 멈춤')}, 3000) // 미리 변수명을 지정해야 한다
clearTimeout(timer)

 

 

일정 시간마다 할 일을 반복

// setInterval(function(){}, time)
setInterval(function(){alert('3초마다 반복된다')}, 3000)

// 멈추기
var timer2 = setInterval(function(){alert('3초마다 반복된다')}, 3000) // 마찬가지로 변수명 지정해준다.
clearInterval(timer2)

 

Comments