I checked SO and found this answer link and wrote my solution link , but in reality it doesn't work.
Task:
- Writing multiple blocks and adding a mouseover event listener to them.
- After the event is fired, it will be processed with a debounce.
- Displaying an event in the console
My сode:
const cards = document.getElementsByClassName('card-wrapper');
function onMouseMoveHandler(e) {
return debounce(() => update(e));
}
function debounce(func) {
let isCan = true;
return () => {
if (isCan) {
func();
isCan = false;
setTimeout(() => isCan = true, 500);
}
}
}
function update(e) {
console.log(e);
}
for (let i = 0; i < cards.length; i++) {
cards[i].onmousemove = onMouseMoveHandler;
}
body {
display: flex;
}
.card-wrapper {
margin: 2%;
width: 300px;
height: 150px;
}
.card {
width: 100%;
height: 100%;
background: blue;
}
<div class="card-wrapper card-1">
<div class="card"></div>
</div>
<div class="card-wrapper card-2">
<div class="card"></div>
</div>