JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<h1>Studying JavaScript</h1>
</body>
JavaScript
var i = 0;
// setInterval을 사용하는 경우
/*var timerId1 = setInterval(function(){
console.log(++i);
if(i >= 5){
clearInterval(timerId1);
alert('Time is over!');
}
}, 1000);
*/
// setTiemout을 사용하는 경우.
function plusOne(){
console.log(++i);
var timerId2 = setTimeout(plusOne, 1000);
if( i >= 5){
clearTimeout(timerId2);
alert('Time is over!');
}
} plusOne();