Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created July 26, 2017 04:07
Show Gist options
  • Save lastday154/fbf82a228bafced399eea9fe14f414f1 to your computer and use it in GitHub Desktop.
Save lastday154/fbf82a228bafced399eea9fe14f414f1 to your computer and use it in GitHub Desktop.
How to have a mouseover event fire only if the mouse is hovered over an element for at least 1 second?
var delay = function (elem, callback) {
var timeout = null;
elem.onmouseover = function() {
// Set timeout to be a timer which will invoke callback after 1s
timeout = setTimeout(callback, 1000);
};
elem.onmouseout = function() {
// Clear any timers set to timeout
clearTimeout(timeout);
}
};
delay(document.getElementById('someelem'), function() {
alert("Fired");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment