day13

์ƒ์€๐Ÿ‘ธยท2023๋…„ 11์›” 20์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ๋‘๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
1/13
post-thumbnail
post-custom-banner

๐Ÿ“– ์ด๋ฒคํŠธ์—์„œ this ํ™œ์šฉํ•˜๊ธฐ

this : ์ด๋ฒคํŠธ ๋ฐœ์ƒํ•œ ์ž๊ธฐ์ž์‹  ์š”์†Œ
()=> ํ™”์‚ดํ‘œํ•จ์ˆ˜ -> ์ด๋ฒคํŠธ.target(๋ฐœ์ƒ์š”์†Œ)
(e)=> -> e.target // ํ™”์‚ดํ‘œ:target

forEach() => ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๊ฐ€์ง„ ์š”์†Œํ•œํ…Œ ์“ธ ์ˆ˜ ์žˆ๋Š”
function() -> this

<style>
	.box {
  		width: 100px;
        height: 100px;
        border: 1px solid black;
        margin: 30px;
    }
  
  	.bgColor-red {
  		background-color: red;
  	}
		
</style>

    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
<script>
	const boxes = document.querySelectorAll('.boxes');

	boxes.forEach( //boxes์˜ ๊ฐ๊ฐ item
    	(item)=> {
        	item.addEventListener('click', function(){
              //this.style.backgroundColor = 'red';
            this.classList.toggle('bgColor-red');
            }); // on,off๊ธฐ๋Šฅ ! 
        }
      )

	for (let item of boxes) {
    	item.addEventListener('click', (e)=>{
        	e.target.style.backgroundColor = 'red';
        });
    }
</script>

=> ๐Ÿ’ป


=> ๊ฐ๊ฐ ๋ฐ•์Šค๋ฅผ ํด๋ฆญํ•˜๋ฉด red๋กœ ๋ฐ”๋€Œ๊ณ  ๋˜ ํด๋ฆญํ•˜๋ฉด ์›๋ž˜์ƒํƒœ๋กœ ๋Œ์•„์˜จ๋‹ค!

๐Ÿ“– window ๊ฐ์ฒด ํ™œ์šฉ - ์ฃผ์š” ์†์„ฑ ๋ฐ ํ•จ์ˆ˜

๐Ÿ– @ ์‹œ๊ฐ„ ๊ธฐ์ค€ : ms 1000 -> 1์ดˆ
setTimeout(ํ•จ์ˆ˜, ์ง€์—ฐ์‹œ๊ฐ„ms) : ์ผ์ • ์‹œ๊ฐ„ ๋’ค์— ํ•จ์ˆ˜ ์‹คํ–‰
setInterval(ํ•จ์ˆ˜, ์‹œ๊ฐ„๊ฐ„๊ฒฉms) : ์ผ์ • ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ๊ณ„์† ํ•จ์ˆ˜ ์‹คํ–‰
clearInterval(๋Œ€์ƒ์ธํ„ฐ๋ฒŒ) : Interval ๊ฑธ๋ฆฐ๊ฑธ ํ•ด์ œ

<style>
	#box {
		width: 100px;
        height: 100px;
        border: 1px solid black;
        position: relative;
        left: 0px;
    }
</style>

<button id="btn1">timeout</button>
<button id="btn2">interval</button>
<button id="btn3">clearinterval</button>
<div id="box"></div>
<script>
	const box = document.getElementById('box');

  	document.getElementById('btn1').addEventListener('click', ()=>{
		setTimeout(()=>{
    		box.style.backgroundColor = 'red';
    	}, 1000); //btn1 ๋ˆ„๋ฅด๋ฉด 1์ดˆํ›„์— box๊ฐ€ red๋กœ ๋ฐ”๋€œ
	});

	let box_left = 0;
	let boxMoveInterval; //๊ฐ™์ด์“ฐ์ด๋ ค๋ฉด ์ „์—ญ๋ณ€์ˆ˜๋กœ ์“ฐ์—ฌ์•ผํ•จ
	document.getElementById('btn2').addEventListener('click', ()=>{
    	boxMoveInterval = setInterval(()=>{ // ์–ด๋–ค ์ธํ„ฐ๋ฒŒ์„ ํด๋ฆฌ์–ดํ•˜๋Š”์ง€ ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋กœ ๋‹ด์•„๋‘”๋‹ค
        	box_left += 10;
            box.style.left = box_left + "px"; //1์ดˆ๋งˆ๋‹ค 10px์”ฉ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
        }, 1000);          
    });

	document.getElementById('btn3').addEventListener('click', ()=> {
   		clearInterval(boxMoveInterval); //ํด๋ฆฌ์–ด ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„๋•Œ ๋ฉˆ์ถฐ์ง€๊ฒŒ๋”!
	});
</script>

=> ๐Ÿ’ป

=> timeout์„ ๋ˆ„๋ฅด๋ฉด 1์ดˆ ํ›„์— ๋ฐ•์Šค๊ฐ€ red๋กœ ๋ฐ”๋€Œ๊ณ 

=> ๐Ÿ’ป

=> interval์„ ๋ˆ„๋ฅด๋ฉด 1์ดˆ๋งˆ๋‹ค ์™ผ์ชฝ์œผ๋กœ 10px์”ฉ ์ด๋™

=> ๐Ÿ’ป

=> clearInterval์„ ๋ˆ„๋ฅด๋ฉด ์ •์ง€!

๐Ÿšจ ์ธํ„ฐ๋ฒŒ ๋ˆ„๋ฅผ๋•Œ๋งˆ๋‹ค ์ธํ„ฐ๋ฒŒ์ด ์Œ“์•„์ ธ๊ฐ€๋Š”๊ฑฐ์•ผ! ์ค‘๋ณต์‹คํ–‰์ด ๋œ๋‹ค
๊ทธ๋ž˜์„œ ์ธํ„ฐ๋ฒŒ ์ƒ์„ฑ ์ „์— ๊ธฐ์กด ์ธํ„ฐ๋ฒŒ ํด๋ฆฌ์–ด๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค
!

let box_left = 0;
let boxMoveInterval; //๊ฐ™์ด์“ฐ์ด๋ ค๋ฉด ์ „์—ญ๋ณ€์ˆ˜๋กœ ์“ฐ์—ฌ์•ผํ•จ
	document.getElementById('btn2').addEventListener('click', ()=>{
    ๐ŸšจclearInterval(boxMoveInterval);๐Ÿšจ
    boxMoveInterval = setInterval(()=>{ // ์–ด๋–ค ์ธํ„ฐ๋ฒŒ์„ ํด๋ฆฌ์–ดํ•˜๋Š”์ง€ ๋ช…์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋กœ ๋‹ด์•„๋‘”๋‹ค
     	box_left += 10;
        box.style.left = box_left + "px"; //1์ดˆ๋งˆ๋‹ค 10px์”ฉ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
    }, 1000);          
});
profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€