Skip to content

Instantly share code, notes, and snippets.

@WuglyakBolgoink
Forked from kuldipem/app-exit.js
Created June 7, 2018 11:04
Show Gist options
  • Save WuglyakBolgoink/5fa8f5da4daf09014f7d18b1a6e1f745 to your computer and use it in GitHub Desktop.
Save WuglyakBolgoink/5fa8f5da4daf09014f7d18b1a6e1f745 to your computer and use it in GitHub Desktop.
Cordova/PhoneGap exit app after twice press back button with toast ( plugin required https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin )
var lastTimeBackPress=0;
var timePeriodToExit=2000;
function onBackKeyDown(e){
e.preventDefault();
e.stopPropagation();
if(new Date().getTime() - lastTimeBackPress < timePeriodToExit){
navigator.app.exitApp();
}else{
window.plugins.toast.showWithOptions(
{
message: "Press again to exit.",
duration: "short", // which is 2000 ms. "long" is 4000. Or specify the nr of ms yourself.
position: "bottom",
addPixelsY: -40 // added a negative value to move it up a bit (default 0)
}
);
lastTimeBackPress=new Date().getTime();
}
};
document.addEventListener("backbutton", onBackKeyDown, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment