首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对象属性返回NaN,然后使对象函数触发。

问对象属性返回NaN,然后使对象函数触发。
EN

Stack Overflow用户
提问于 2017-02-21 01:31:01
回答 1查看 122关注 0票数 0

我正在练习Javascript,并尝试编写一个对象,当在网页中引用时,该对象将监视用户并在他不活动时将其注销。

但是,该对象不按预期工作。我试着查看属性minsBeforeForceLogout,它返回NaN

我尝试在setTimeout之前将属性设置为0,因为我已经读过,在声明时,对象还没有值,但仍然没有增量。我怎么解决这个问题?我使用Javascript和Jquery的混合。

编辑我已经解决了NaN问题,但是当我运行注销事件时不会对我的if触发。我的情况有什么问题吗?

编辑2我用setInterval代替了setTimeOut

代码语言:javascript
复制
$(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); });

为了澄清,我正在尝试函数注销,这意味着页面会说:“您由于不活动而被注销了。”

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-21 01:53:55

在你的定义中,你有minsBeforeForceLogOut,而在其他一些地方,你有minsBeforeForceLogout --不同的骆驼壳。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42357141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档