함수 - Interval/Timeout, String

heehe·2023년 2월 10일
0

JavaScript

목록 보기
8/8
post-thumbnail

1초후 계속 반복 (시간)

setInterval(sayHello, 1000);
sayHello() 라는 펑션을 1초마다 반복한다는 의미.
단 바로 실행되지 않고 1초 후 첫 시작이 되고 계속 1초마다 반복된다.

setTimeout(sayHello, 1000);
1초 기다렸다가 한번만 실행.


number를 string으로 바꾸는 방법

String() 안에 감싸는 방법이 있다.
0 > 00 로 바꾸고 싶을 때는 padStart(x, 'y') 를 사용하면 된다.
padStart는 내가 갖고 있는 string 을 길게 만들어 주고 싶을 때 사용.
뒤에서 추가하고 싶을 때는 padEnd를 사용하면 된다.
x는 원하는 length를, y는 앞에 채워줄 내용을 입력하면 된다.
String(new Date().getHours()).padStart(2,'0');


background 랜덤하게 출력

const chosenimage = images[Math.floor(Math.random() * images.length)];
//imges에서 random하게 출력

const bgimg = document.createElement("img");
// html을 위한 img태그를 js에서 만들 수 있다.

bgimg.src = `img/${chosenimage}`;
// string과 변수를 연결해주는 방법

document.body.appendChild(bgimg);
// html body에 bgimg를 추가해준다.

profile
성장하고픈 ISFJ

0개의 댓글