<!DOCTYPE html>
<html lang="de">
<head>
<!-- UTF-8 Prüfkommentar -->
<title>Testseite</title>
<!-- META -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- Stylesheet -->
<style>
div {
font-size: 5em;
text-align: center;
float: left;
cursor: pointer;
}
div.red {
background-color: red;
width: 100px;
height: 100px;
}
div.green {
background-color: green;
width: 100px;
height: 100px;
}
</style>
<!-- JavaScript -->
<script>
function addEvent(obj, type, fn ) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false );
} else if (obj.attachEvent) {
obj["e"+type+fn] = fn;
obj[type+fn] = function() {
obj["e"+type+fn]( window.event );
}
obj.attachEvent('on' + type, obj[type + fn]);
}
}
function countClick() {
var now = new Date().getTime(),
time = this.time || 0,
count = this.count || 0,
timeDiff = now - time;
console.log(this.className);
if (time === 0) {
this.time = now;
this.count = 1;
this.innerHTML = 1;
return;
}
count += 1;
if (count >= 5) {
if (timeDiff <= 2000) {
this.innerHTML = count;
alert('Geschafft innerhalb ' + (timeDiff / 1000) + ' Sekunden.');
} else {
alert('Das war zu langsam !!!');
this.innerHTML = 0;
}
this.time = 0;
this.count = 0;
} else {
this.count = count;
this.innerHTML = count;
}
}
function init() {
var i,
divs = document.getElementsByTagName('div');
divsLength = divs.length;
if (divsLength < 0) {
return;
}
for (i = 0; i < divsLength; i += 1) {
addEvent(divs[i], 'click', countClick);
divs[i].innerHTML = 0;
}
}
window.onload = init;
</script>
</head>
<body>
<h1>Klicke innerhalb 2 Sekunden 5 Mal auf eine Fläche !!!</h1>
<br />
<div class="red"></div>
<div class="green"></div>
<div class="red"></div>
<div class="green"></div>
<div class="red"></div>
<div class="green"></div>
<div class="red"></div>
<div class="green"></div>
</body>
</html>