document.addEventListener('mouseover', ({ target: t }) => {
if (t.classList.contains('pic') && !t.dataset.interval) {
t.dataset.interval = bgRotate(t);
}
});
document.addEventListener('mouseout', ({ target: t }) => {
const { interval } = t.dataset;
if (interval) {
clearInterval(interval);
delete t.dataset.interval;
}
});
или
document.querySelectorAll('.pic').forEach(n => {
n.addEventListener('mouseenter', onHover);
n.addEventListener('mouseleave', onHover);
});
function onHover({ type, target: t }) {
const d = t.dataset;
if (type === 'mouseenter' && !d.interval) {
d.interval = bgRotate(t);
} else if (type === 'mouseleave') {
clearInterval(d.interval);
delete d.interval;
}
}