JSFiddle - React, Tailwind, and code Playground
HTML
<h2 class="step step1">Test</h2>
CSS
.active {
background-color: #C00;
color: #FFF;
}
JavaScript
function debounce (fn, delay) {
var timer = null;
var start = + new Date();
return function () {
var current = + new Date()
if(current-start > delay) {
fn.apply(this, arguments);
start = current;
}
};
};
document.onkeydown = debounce(function(e) {
switch (e.keyCode) {
case 37:
console.log("left");
break;
case 38:
console.log("up");
break;
case 39:
console.log("right");
break;
case 40:
console.log("down");
break;
}
}, 1000);