我正在练习Javascript,并尝试编写一个对象,当在网页中引用时,该对象将监视用户并在他不活动时将其注销。
但是,该对象不按预期工作。我试着查看属性minsBeforeForceLogout,它返回NaN
我尝试在setTimeout之前将属性设置为0,因为我已经读过,在声明时,对象还没有值,但仍然没有增量。我怎么解决这个问题?我使用Javascript和Jquery的混合。
编辑我已经解决了NaN问题,但是当我运行注销事件时不会对我的if触发。我的情况有什么问题吗?
编辑2我用setInterval代替了setTimeOut
$(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); });为了澄清,我正在尝试函数注销,这意味着页面会说:“您由于不活动而被注销了。”
发布于 2017-02-21 01:53:55
在你的定义中,你有minsBeforeForceLogOut,而在其他一些地方,你有minsBeforeForceLogout --不同的骆驼壳。
https://stackoverflow.com/questions/42357141
复制相似问题