2017-02-21 114 views
0

我正在練習Javascript,並嘗試編寫一個對象,該對象在網頁中引用時將監視用戶並在他不活動時將其註銷。對象屬性返回NaN,然後​​使對象函數消防

但是,該對象不能按預期工作。我試着查看屬性minsBeforeForceLogout並且它返回NaN

我試圖在setTimeout之前將該屬性設置爲0,因爲我已經讀過在聲明對象沒有值但尚未增量的位置。我能解決這個問題嗎?我使用JavaScript和jQuery

編輯 我已經得到解決的NaN問題的混合,但是當我運行註銷事件不會觸發我如果。我的病情有問題嗎?

EDIT 2 我已經取代setTimeOutsetInterval

$(document).ready(function() { 


/*below object mainly checks if user is inactive*/ 
var monitorIfActive = { 
    minsBeforeForceLogOut : 0, //if this variable reaches threshold, logout commences 
    threshold: 5, 

    resetInactivity : function(e) { 
     monitorIfActive.minsBeforeForceLogOut = 0;   
    }, 

    userIsInactive : function() { 
     monitorIfActive.minsBeforeForceLogout++; 
     swal(monitorIfActive.minsBeforeForceLogout.toString());// <- **returns NaN** 

     if (monitorIfActive.minsBeforeForceLogout >= monitorIfActive.threshold) 
     { 
      monitorIfActive.logout(); 
     } 

    }, 

    logout: function() { 
     swal("You have been logged out due to inactivity"); 
    } 


} 

//bind to html 
$('body').on('keypress click mousemove scroll', monitorIfActive.resetInactivity); 


//start the timer countdown for our monitor object. 
monitorIfActive.minsBeforeForceLogOut = 0; //<- I tried this, but still it does not work. 
var startMonitoring = setTimeout(monitorIfActive.userIsInactive, 1000); }); 

爲了澄清,我試圖去功能註銷,這意味着頁面會說「您已登出由於活動

回答

2

在你的定義中,你有minsBeforeForceLogOut,而在其他一些地方你有minsBeforeForceLogout - 不同camelcasing。

+0

謝謝,這已經解決了NaN問題..但任何想法如何讓這個對象觸發註銷功能? –

+1

@ Malky.Kid當然。使用'setInterval'而不是'setTimeout' - setTimeout只觸發一次。 – JSilv

+0

非常感謝Jsilv。 –