2013-04-10 56 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

相關問題