Math.random/ setInterval/ clearInterval in javascript // 자바스크립트 Math.random/ setInterval/ clearInterval 이해하기
<script>
var nCount=0;
var panel=null;
window.onload=function(){
this.init();
}
function init(){
this.panel=document.getElementById("panel");
}
var t;
function start(){
t = setInterval(this.addTag,20);
//0.02초에 한번씩 addtag 출력
}
function addTag(){
this.nCount++;
var span=document.createElement("span");
span.style.color="#"+(parseInt(Math.random()*0xffffff)).toString(16);
span.style.fontSize=(10+parseInt(Math.random()*40))+"px";
span.style.display="inline-block";
span.innerHTML=this.nCount;
this.panel.appendChild(span);
console.log(nCount);
}
function stopButton(){
clearInterval(t);
}
</script>
</head>
<body>
<div>
<button onClick="start()">start</button>
<button onClick="stopButton()">stop</button>
<div id="panel"></div>
</div>
</body>
</html>
댓글
댓글 쓰기