Notice
Recent Posts
Recent Comments
Link
«   2026/06   »
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
관리 메뉴

학습기록남기기

2022_05_31_JS_7일 본문

수업_정리

2022_05_31_JS_7일

backend_na 2022. 5. 31. 22:56
const $form=document.regForm;// 폼 객체 얻는 방법
$form.id.value // 폼 객체에 접근 

/////////////////////////////////////
날짜 
let date=new Date();// 오늘 날짜를 가진 객체

console.log(date);
console.log(date.getTime());//날짜를 밀리초로 구함
console.log('-----------------------');
const millis=1573735023000;
const today=new Date(millis); 
console.log(today.getTime()); //생성자에 넘겨준 값 그대로나옴
console.log(today);//생성자에 넘겨준 값을 날짜 형태로 변환해서 나옴 

console.log(today.getFullYear()); //년
console.log(today.getMonth()+1); //월
console.log(today.getDate()); //일
console.log(today.getHours());//시
console.log(today.getMinutes());//분
console.log(today.getSeconds());//초

///////
인터벌 
const stop=setInterval(function(){
            console.log(1);
        },500);

        //clearIntervale(정지할 인터벌함수) 는 인터벌을 중지합니다.
        document.querySelector('.stopInterval').onclick=function(){
            clearInterval(stop); //인터벌함수 멈추기
        }

/////
쿠키
//쿠키 생성하기 
    function createCookie(){
        const name=document.getElementById('cookieName').value;
        const value=document.getElementById('cookieValue').value;

        //쿠키의 유효시간 설정 :날짜 객체 필요
        const date=new Date();

        date.setDate(date.getDate()+7);//현재 시간으로부터 7일 뒤 세팅  
        // setSeconds(date.getSeconds()+10) : 10초뒤까지 쿠키 유지
        // setMonth(date.getMonth()+1) //한달까지 쿠키 유지 

        
        //쿠키에 저장할 문자열 생성 : 주의사항 [형식을 반드시 지켜야 한다.] : 이름 +'='+값+';'   고정!! 키=값 ;  
        let cookie='';
        cookie+=name+'='+value+';';  //키=값; 설정 ::세미콜론도 작성해야함  
        cookie+='expires='+date.toUTCString(); // 쿠키의 유효시간 설정 
        
        //쿠키에 저장
        document.cookie=cookie;
    }

    //쿠키 확인하기 
    const $get=document.getElementById('get');
    $get.addEventListener('click',getCookie);

    function getCookie(){
        const find=document.getElementById('cookieFind').value;//찾을 쿠키 이름 
        console.log(document.cookie); //생성된 쿠키 확인 
        const cookies=document.cookie.split(';');
        console.log(cookies);

        let flag=false;
        for(let c of cookies){
            if(c.search(find) !==-1){  //search 메서드는 값을 찾으면 ,찾은 위치를 반환  없으면 -1을 반환 
                console.log(c.replace(find+'=','')); //쿠키의 이름을 지우기 위해 빈 문자열로 대체 
                flag=true;
                break;
            }
        }

'수업_정리' 카테고리의 다른 글

2022_06_03_Spring_2일  (0) 2022.06.03
2022_06_02_Spring_1일  (0) 2022.06.02
2022_05_30_JS_6일  (0) 2022.05.31
2022_05_27_JS_5일  (0) 2022.05.27
2022_05_26_JS_4일  (0) 2022.05.26