2017-02-21 7 views
0

JavaScriptを練習し、Webページで参照されたときにユーザーを監視し、彼がアクティブでない場合にログアウトするオブジェクトをコーディングしようとしました。ObjectプロパティがNaNを返した後、オブジェクトファンクションを起動します

ただし、オブジェクトは意図したとおりに動作しません。どのように..私はプロパティminsBeforeForceLogoutを見てみました、それは私が、宣言の時点でオブジェクトがまだ値を持っていないことを読んだので、私はsetTimeoutを前に0にプロパティを設定しようとしたが、まだそれは増加しませんNaN

を返します。私はこれを解決できますか?私はJavascriptをとjQuery

EDIT私は解決のNaN問題を持っている のミックスを使用していますが、私はログアウトイベントを実行したときに、私の場合には発生しません。私の状態に何か問題はありましたか?私は明確にするためsetInterval

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

setTimeOutを交換した

EDIT 2 が、私はページを意味し、ログアウトを機能するように取得しようとしています "あなたは不活動によるログアウトされましたを言うだろう "

答えて

2

あなたの定義にはminsBeforeForceLogOutがありますが、他の場所にはminsBeforeForceLogoutのキャメルケーシングがあります。

+0

ありがとう、それはNaNの問題を解決しました..しかし、どのように私はこのオブジェクトをログアウト機能を発火させることができますか? –

+1

@ Malky.Kid 'setTimeout'の代わりに' setInterval'を使用してください。setTimeoutは1回だけ起動します。 – JSilv

+0

Jsilvありがとうございました。 –

関連する問題