<html>

<head>

<script type="text/javascript">


var myTime;

window.onload = function(){

var clock = document.getElementById('clock');


startTime();

}; 


function stopTime(){

clearInterval(myTime);

}

// 윈도우 타이머

function startTime(){

myTime = setInterval(function(){

clock.innerHTML = new Date().toString();

}, 1000);

}

// 클로저 예, 지역변수 남겨두는 현상, 리턴된 함수 자체

function test(name){

var output = 'Hello ' + name + ' ... !';

return function () {

alert(output);

};

}

test('JavaScript')();

var test1 = test('Web');

test1();


function nextRandomInteger(limit){

return Math.round(Math.random()*limit);

}


// 클로저 활용, 메모리 효율적

var randomAlphabet = function(){

var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

return function(){

return alphabet.charAt(nextRandomInteger(25));

}

}();

window.onload = function(){

document.body.innerHTML += randomAlphabet();

};



</script>

</head>

<body>

<h1 id='clock'></h1>

<button onClick='stopTime();'>Stop</button><br/>

<button onClick='startTime();'>Start</button><br/>

</body>

</html>



+ Recent posts