添加事件以防止Inappbrowser内超时

Add event to prevent timeout inside Inappbrowser

本文关键字:Inappbrowser 超时 事件 添加      更新时间:2023-09-26

我有一个超时功能

var idleTimeOut = 60; // 1 minute
ResetIdleSecondsEvent = function () {
    // Reset counter for all listed event
    $(document).on("click mousemove keypress touchstart touchend touchcancel touchmove", function () {            
        _idleSecondsCounter = 0;
    })      
}        
  function VerifyTimeOutInterval() {
    // Run every seconds to call function and check if current counter is more than
    // the preset time
    window.setInterval(CheckIdleTime, 1000);
}    
 function CheckIdleTime() {        
    // Check timer only predefined timeout value is not 0, null or empty
    if (idleTimeOut != "") {
        // Increase counter for seconds
        _idleSecondsCounter++;
        // Check if current counter is more than the preset timeout period
        if (_idleSecondsCounter >= idleTimeOut) {
            // Notify user that he/she has been automatically logged out
            navigator.notification.alert("You have been automatically logged out due " +
                "to inactivity. Please login again.", function () { }, "Logged Out", 'OK');
            // Navigate to login page
            self.navigateLogout();
        }
    }      
}     

这在我的应用程序中非常有效,当用户在1分钟内无所事事时,会出现超时消息,并将用户带到登录屏幕。

但是,当用户使用Inappbrowser打开外部链接并有活动时,空闲秒数没有重置,会出现超时消息,并让用户重新登录。那么,如何将相同的代码添加到Inapp浏览器中呢?

据我所知,Inapp浏览器只有事件loadstart、loadstop、loaderror和exit。我尝试了以下代码将事件添加到Inappbrowser中,但没有成功。

var inappBrowserObj = window.open(url, '_blank', 'location=yes');
inappBrowserObj.addEventListener('loadstop', ResetIdleSecondsEvent());
inappBrowserObj.addEventListener('loadstop', VerifyTimeOutInterval());

请告诉我在Inappbrowser中处理超时功能的正确方法是什么。

感谢

在实现事件处理程序时,您的代码似乎是错误的。回调传递不正确,而且不需要为同一事件注册两次处理程序(尽管可以但不是必需的)。您的代码应该是:

inappBrowserObj.addEventListener('loadstop', ResetIdleSecondsEvent);

观察我是如何传递回调的,您的代码将执行该函数,而不是将其作为回调传递。

ResetIdleSecondsEvent函数中进行其他函数调用。

我希望这能有所帮助。