我正在练习Javascript并尝试编写一个对象,当在网页中引用时,它将监视用户并在他处于非活动状态时将其注销。
但是,该对象不能按预期工作。我尝试查看属性minsBeforeForceLogout
,然后返回NaN
我尝试在setTimeout之前将属性设置为0,因为我已经读过声明对象还没有值,但是它仍然没有增加..我怎么解决这个问题?我正在使用Javascript和Jquery的混合
修改 我已经解决了NaN问题,但是当我运行注销事件时,我的if不会触发。我的病情有问题吗?
编辑2
我已将setTimeOut
替换为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); });
为了澄清,我试图进行功能注销,这意味着页面会说&#34; 您因为不活动而被注销&#34;
答案 0 :(得分:2)
在您的定义中,您拥有minsBeforeForceLogOut
,而在其他一些地方minsBeforeForceLogout
- 不同的地方。