Help, do not go into my head how to implement. When the user does not move the mouse for a few seconds on the div ʻe, on which the mousemove event was present, the event was called while the mouse was still. Simply put, I need a mousemove antonym.
- 2At the mouse event, you remember the time of the last event. And you hang up the timer, which checks this time every few seconds, if enough time has already passed since the last event, then what happened was expected. If not enough time has passed, then we plan the next execution of the timer in N seconds minus the number of times since the last event (so that the timer would work on time). Or, as an option at the mouse event, just reset the previous timer and start a new one - Mike
- Thanks for the advice, helped to some extent) - Vladyslav Semenyuk
|
1 answer
var meBlock = document.getElementById('me-block'), t; meBlock.addEventListener('mousemove', function(e) { meBlock.style.backgroundColor = 'green'; meBlock.style.color = "#fff"; meBlock.innerHTML = 'МЫША БЕГАЕТ'; clearTimeout(t); t = setTimeout(function() { meBlock.style.backgroundColor = 'red'; meBlock.innerHTML = 'МЫША СТАЛ'; }, 1000); }); meBlock.addEventListener('mouseout', function(e) { meBlock.style.backgroundColor = 'transparent'; meBlock.style.color = "#000"; meBlock.innerHTML = 'МЫША НЕТЬ'; clearTimeout(t); }) #me-block { width: 200px; height: 100px; border: 1px solid #000; } <div id="me-block"></div> - Pasib, already decided, about the same) - Vladyslav Semenyuk
|