본문 바로가기
Java Script

Clock

by _션샤인 2024. 8. 26.

5.0 Intervals

1) Interval : something that should happen every 'n'second.

2) example

 

function sayHello() {

  console.log("hello");

}

 

-> If you want to execute this function every 2seconds.

 

3) setInterval

 - takes two argument.

(1)  function that you want to run

(2) the miliseconds that you want to wait between each call

 

 setInterval(sayHello, 5000ms) = every 5second

 

 

 

 

 


5.1 Timeouts and Dates

 

1) setTimeout(function we want to run, times you will wait);

2) new Date()

 -> get the documentation Date

 

const date = new Date();

date.getFullYear()

date.getHours()

date.getMinutes()

date.getSeconds()

 

 

function getClock() {

const date = new Date();

console.log(`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`);

 

getClock()

setInterval(getClock, 1000);

 

 

 

 

 


5.2 PadStart

1) "1".padStart(2, "0")

this string should be two characters long,

and if it is not two characters long

we want to add '0' infront

until it becomes two characters long.

->01

 

2) "1".padEnd(2,"0")

this string should be two characters long,

and if it is not two characters long

we want to put '0' 

->10

 

3) Change numbers to string

new Date().getHours -> Number

String(new Date().getHours()) -> String

 

 * string to number -> parseInt();

 

 

 

'Java Script' 카테고리의 다른 글

To do list (1)  (0) 2024.08.28
Quotes  (0) 2024.08.27
Login Form  (0) 2024.08.23
Javascript on the browser  (0) 2024.08.22
Welcome to Javascript  (0) 2024.08.02