2013-04-10 59 views
0

我有以下代码:按钮长按退出Android上的PhoneGap应用

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);  
} 

当用户等待5秒(长按)它开始下面的函数之后按下Exit_btn函数注销() {

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

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

}

的麻烦的是,它不能顺利运行,这意味着如果我输入了错误的密码,提示框不断弹出,或者如果我终于正常退出应用程序,那么当我再次启动应用程序时,它会崩溃。

任何人都可以在这里看到问题吗?为什么会发生?

任何帮助表示赞赏。

谢谢。

回答

1

你可以使用jQuery Mobile的taphold事件,如下面...这可以帮助你......

HTML:

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

jQuery Mobile的:

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

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

长按注销按钮750毫秒,它会调用logout()。

默认情况下,如果要通过分配值to $.event.special.tap.tapholdThreshold来更改点按时间的数量,则点击持续时间为750毫秒。如下所示...

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

谢谢你的回答。我试图实现这一点,但不幸的是,logout()函数从未被调用过。我不明白为什么.. – OlejkaKL 2013-04-10 11:34:16

+0

@ user1756004:你的logout()函数只是没有得到调用,或者taphold事件回调函数本身没有被调用..? – 2013-04-10 12:02:46

+0

taphold事件回调函数没有被调用。我在那里设置了一个警报,它没有反应。 – OlejkaKL 2013-04-10 12:18:18

相关问题