简体   繁体   English

长按按钮可退出Android PhoneGap上的应用

[英]Button long press to exit the app on Android PhoneGap

I have the following code: 我有以下代码:

function logout_now()

//Logout of the app after a long press onKey(Longer then 5 sec)    Not working correctly

{
var startTime; 
var endTime;
var TimeDiff;

document.getElementById('Exit_btn').addEventListener('touchstart',function(event)
        {startTime = new Date().getTime();
        },false);

document.getElementById('Exit_btn').addEventListener('touchend',function(event){
        endTime = new Date().getTime();
        TimeDiff = endTime-startTime;   

        if( endTime-startTime > 5000 )  //logout after more then 5 Second = 5000 mSec
            {
            logout();      
            }
        },true);     
 }

When the user presses the Exit_btn after waiting 5 seconds (long press) it starts the following function: 当用户等待5秒(长按)后按下Exit_btn时,它将启动以下功能:

function logout() { 函数logout(){

var password = prompt("Please enter the exit password");

if (password == "123")
     {
        alert("Goodbye");
        navigator.app.exitApp();
     }
else
     {
        alert("Wrong Password!");
        console.log("index.html");
     }

} }

The trouble is that it doesn't work smooth, meaning if I enter the wrong password the prompt box keep popping up, or if I finally exit the app properly, when I launch it again it crashes. 问题在于它无法正常运行,这意味着如果我输入了错误的密码,提示框将继续弹出,或者如果我终于正确退出了应用程序,则当我再次启动它时,它会崩溃。

Can anyone see the problem here? 有人可以在这里看到问题吗? Why does it happen? 为什么会发生?

Any help appreciated. 任何帮助表示赞赏。

Thanks. 谢谢。

You could use jQuery Mobile taphold event, like below... this may help you... 您可以使用jQuery Mobile Taphold事件,如下所示...这可能对您有帮助...

Html: HTML:

<div id="logout-btn">Logout</div>

jQuery Mobile: jQuery Mobile:

$(function() {
   $( "#logout-btn" ).on('taphold', tapholdCallBack);
     // Callback function
     function tapholdCallBack(ev) {
        logout();
        .....
     }
});

or 要么

$(document).delegate('div[data-role*="page"]', 'pageshow', function () {
  $(document).delegate('#logout-btn', 'taphold', function (ev) {
    logout();
  });
});

Long press the logout button for 750 milliseconds, it will call logout(). 长按注销按钮750毫秒,它将调用logout()。

By default tap duration is 750ms, if you want change the amount of time a tap should by assigning a value to $.event.special.tap.tapholdThreshold . 默认情况下,抽头持续时间为750ms,如果您想通过将值分配to $.event.special.tap.tapholdThreshold更改抽头应花费的时间。 like shown below... 如下图所示

 $(document).bind("mobileinit", function () {
    $.event.special.tap.tapholdThreshold = 5000,
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM