Skip to content

Instantly share code, notes, and snippets.

@frydlewicz
Last active March 8, 2020 18:04
Show Gist options
  • Save frydlewicz/e949667521a1217bd450b20bef4dcfb9 to your computer and use it in GitHub Desktop.
Save frydlewicz/e949667521a1217bd450b20bef4dcfb9 to your computer and use it in GitHub Desktop.
Call function when user wants to leave the website
(function(g,e,c){if(typeof g!="function"){return}var d=false;var f=0;var b=function(){var h=(new Date).getTime();if(d&&(f==0||e&&h-f>c)){document.removeEventListener("mouseleave",b);f=h;g()}};var a=function(j){var h=innerHeight/10;var i=j.movementY;var k=j.clientY;if(i<0&&k<h){d=true;document.addEventListener("mouseleave",b)}else{document.removeEventListener("mouseleave",b);d=false}};document.addEventListener("mousemove",a)})(
function () { // callback function
alert("Do not leave!")
},
true, // repeat calling
5000 // interval in milliseconds
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment