HTML DOM timeStamp 속성은 이벤트가 생성되거나 트리거된 경과 시간을 밀리초 단위로 반환합니다.
참고:timeStamp는 이벤트 시스템이 특정 이벤트에 대해 지원하는 경우에만 작동합니다.
다음은 구문입니다 -
시간 값 반환(밀리초)
event.timeStamp
HTML DOM 타임스탬프의 예를 살펴보겠습니다. 속성 -
예시
<!DOCTYPE html>
<html>
<head>
<title>timeStamp Event</title>
<style>
* {
padding: 2px;
margin:5px;
}
form {
width:70%;
margin: 0 auto;
text-align: center;
}
#outer {
width:70%;
margin: 0 auto;
padding: 0;
text-align: center;
border:1px solid black;
height: 105px;
background-color: #28a745;
}
input[type="button"] {
border-radius: 10px;
}
#upper {
border-bottom: 1px solid black;
height: 40px;
margin: 0 0 15px 0;
background-color: #DC3545;
}
#lower {
border-top: 1px solid black;
height: 40px;
margin: 15px 0 0 0;
background-color: #DC3545;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>timeStamp-Event</legend>
<div id="outer">
<div id="upper"><h2>Danger</h2></div>
<div id="lower"><h2>Danger</h2></div>
</div>
<input type="button" id="start" value="Start" onclick="gameStart()">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById('divDisplay');
var gameDisplay = document.getElementById('outer');
function playGame(event) {
var x = event.clientX;
var y = event.clientY;
if(y > 95 && y < 110){
divDisplay.textContent = 'Keep Going!';
if(x === 439){
divDisplay.textContent = 'Congrats! You Did it in '+event.timeStamp+' milliseconds';
gameDisplay.removeEventListener('mousemove', playGame);
}
}
else{
divDisplay.textContent = 'You moved to DANGER area. You loose!';
gameDisplay.removeEventListener('mousemove', playGame);
}
}
function gameStart(){
gameDisplay.addEventListener('mousemove',playGame);
}
</script>
</body>
</html> 출력
'시작'을 클릭한 후 녹색(안전) 영역의 버튼 및 커서 -

'시작'을 클릭한 후 녹색(안전) 영역 끝에 있는 버튼 및 커서 -

'시작'을 클릭한 후 빨간색(위험) 영역의 버튼 및 커서 -
