const updateCoordinates = e => { if(coordinates.x === undefined || coordinates.y === undefined){ coordinates.x = e.x; coordinates.y = e.y; addElement(); } if(Math.abs(coordinates.x - e.x) > 50 || Math.abs(coordinates.y - e.y) > 50) { coordinates.x = e.x; coordinates.y = e.y; addElement(); } } header.addEventListener('mousemove', e => { updateCoordinates(e); });