JavaScript ๊ธฐ๋ณธ ํจ์ ์ค setTimeout() ์ ๋ํด์ ์์๋ณด์
์ด๊ฑด n์ด ํ ์ฝ๋๋ฅผ ์คํํด์ค๋ค.
setTimeout(function() { ์คํํ ์ฝ๋} , ์๊ฐ);
์๊ฐ์ ms๋จ์ (1์ด๋ 1000ms)
js008.html์ ๋ณต์ฌํด js010.html์ ๋ง๋ค์ด ๋ด
์๋ค.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<script src="https://code.jquery.com/jquery-3.7.0.min.js" integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin="anonymous"></script>
</head>
<body>
<div class="black-bg1">
<div class="white-bg">
<h4>๋ก๊ทธ์ธํ์ธ์</h4>
<form >
<div class="mb-3">
<input type="text" class="form-control" id="email">
</div>
<div class="mb-3">
<input type="password" class="form-control" id="password">
</div>
<button type="submit" id="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-danger" id="close">๋ซ๊ธฐ</button>
</form>
</div>
</div>
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Offcanvas navbar</a>
<span class="badge bg-dark ms-auto mx-3">Dark ๐</span>
<button class="navbar-toggler" type="button">
<span class="navbar-toggler-icon "></span>
</button>
</div>
</nav>
<ul class="list-group" id="list1">
<li class="list-group-item">An item</li>
<li class="list-group-item">A second item</li>
<li class="list-group-item">A third item</li>
<li class="list-group-item">A fourth item</li>
<li class="list-group-item">And a fifth one</li>
</ul>
<button id="loginBtn" type="button" class="btn btn-primary">
๋ฒํผ
</button>
<script>
$('#loginBtn').on('click', function(){
$('.black-bg1').addClass('show-modal1');
});
$('#close').on('click', function() {
$('.black-bg1').removeClass('show-modal1');
})
$('.navbar-toggler').on('click', function() {
$('.list-group').toggleClass('lg-open');
});
$('form').on('submit', function (e){
console.log(document.getElementById('email').value);
if($('#email').val() == ''){
alert('์์ด๋๋ฅผ ์
๋ ฅํ์ธ์');
e.preventDefault(); // ํผ ์ ์ก ๋ง๋ ์ฝ๋
}
if($('#password').val() == ''){
alert('ํจ์ค์๋๋ฅผ ์
๋ ฅํ์ธ์');
}else if($('#password').val().length <4){
alert('ํจ์ค์๋๋ 4์ ์ด์์ด์ด์ผ ํฉ๋๋ค.');
}
});
//badge ํด๋ฆญ ์ ๊ธ์๋ฅผ Dark > Light ๋ณ๊ฒฝ
var count = 0;
$('.badge').on('click', function (){
count++;
// count += 1;
// count = count+1;
console.log(count);
console.log(count % 2);
if(count % 2 === 0){
$('.badge').html('Dark ๐');
$('.navbar').removeClass('navbar-dark');
$('.navbar').removeClass('bg-dark');
$('.navbar').addClass('navbar-light')
$('.navbar').addClass('bg-light')
}else if(count %2 === 1){
$('.badge').html('Light ๐');
$('.navbar').removeClass('navbar-light');
$('.navbar').removeClass('bg-light');
$('.navbar').addClass('navbar-dark')
$('.navbar').addClass('bg-dark')
}
});
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
</body>
</html>
๋ถํ์ํ console ์ด๋ script๋ ์ญ์ ํ๊ณ ์๋์ ์ฝ๋๋ฅผ html ๋ง์ง๋ง์ ์ถ๊ฐ
<div class="alert alert-danger">
5์ด ์ด๋ด์ ๊ตฌ๋งค ์ ์ฌ์ํ ์ฆ์ !
</div>
setTimeout(function (){
//์คํํ ์ฝ๋
console.log("setTimeout ํ์ธ");
}, 2000)
์ด๋ ๊ฒ ํ๋ฉด 2์ด ๋ค์ console์ 'setTimeout ํ์ธ' ๋ฉ์ธ์ง๊ฐ ์ฐํ๋ค.
2000 ์ ๋ณ๊ฒฝํด์ ์ํ๋ ์๊ฐ์ ๋ฉ์ธ์ง๊ฐ ์ถ๋ ฅ๋๋๋ก ํด๋ณด์.
setInterval(function() {
console.log("setInterval ํ์ธ");
}, 1000);
์ด๋ ๊ฒ ํ๋ฉด 1์ด๋ง๋ค ๋ฉ์ธ์ง๊ฐ ์ถ๋ ฅ๋๋ค.
โจ ์ค์ต. 5์ด ํ์ ์๋์ div ๋ฅผ ์ฌ๋ผ์ง๊ฒ ํด๋ณด์.
<div class="alert alert-danger">
5์ด ์ด๋ด์ ๊ตฌ๋งค ์ ์ฌ์ํ ์ฆ์ !
</div>
setTimeout(function (){
$('.alert').hide();
}, 5000)
๐์ฝ๋ฐฑํจ์ ๋ค๋ฅธ ์ฌ์ฉ๋ฒ
์ง๊ธ๊น์ง addEventListener, setTimeout, setInterval ๋ฑ ์ฝ๋ฐฑํจ์๋ฅผ ์ฌ์ฉํ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ต๋๋ค.
//1. ์ง๊ธ๊น์ง ์ฌ์ฉํ ๋ฐฉ์
setTimeout(function(), 1000){
//์ฝ๋ ์์ฑ
}
//2. ์ด๋ ๊ฒ๋ ์ธ ์์๋ค.
function exampleFun() {
console.log("test");
}
setTimeout(exampleFun, 1000);
โจ ๊ณผ์ . ์์์ ๋ง๋ <div> ์์ 1์ด๋ง๋ค 5๋ผ๋ ๋ฌธ์๋ฅผ 1์ฉ ๊ฐ์ ์ํค๊ณ 0์ด๋๋ฉด <div>๋ฅผ ์๋ณด์ด๊ฒ ํด๋ณด์.
<div class="alert alert-danger">
<span id="sec">5</span>์ด ์ด๋ด์ ๊ตฌ๋งค ์ ์ฌ์ํ ์ฆ์ !
</div>
<script>
var second = 5;
var timer = function() {
console.log(second);
if(second > 0 ){
second--;
$("#sec").html(second);
}else {
$('.alert').hide();
clearInterval(timerVar);
}
};
var timerVar = setInterval(timer, 1000);
</script>