Skip to content

Instantly share code, notes, and snippets.

@tinolove81
Last active March 30, 2020 07:23
Show Gist options
  • Select an option

  • Save tinolove81/03c16e49341ac0db3076a70d36f41bd5 to your computer and use it in GitHub Desktop.

Select an option

Save tinolove81/03c16e49341ac0db3076a70d36f41bd5 to your computer and use it in GitHub Desktop.
<script>
let isTimerSetup = false;
let timeoutId;
let timeoutInMiliseconds = 600;
let selector = 'popup選擇器';
function startTimer() {
timeoutId = window.setTimeout(() => {
$(selector).fadeIn();
}, timeoutInMiliseconds)
}
function resetTimer() {
window.clearTimeout(timeoutId);
startTimer();
}
$(window).on('keydown scroll', () => {
$(selector).fadeOut();
if (isTimerSetup == false) {
isTimerSetup = true;
document.addEventListener("mousemove", resetTimer, false);
document.addEventListener("mousedown", resetTimer, false);
document.addEventListener("keypress", resetTimer, false);
document.addEventListener("touchmove", resetTimer, false);
startTimer();
} else {
resetTimer();
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment