Created
July 26, 2017 04:07
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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