0

我有以下代码:

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

}

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

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

任何帮助表示赞赏。

谢谢。

4

1 回答 1

1

您可以使用 jQuery Mobile taphold事件,如下所示...这可能会对您有所帮助...

html:

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

jQuery 移动:

$(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()。

默认情况下,点击持续时间为 750 毫秒,如果您想通过分配一个值来更改点击的时间量to $.event.special.tap.tapholdThreshold。如下图...

 $(document).bind("mobileinit", function () {
    $.event.special.tap.tapholdThreshold = 5000,
 });
于 2013-04-10T11:07:09.033 回答