对象中的JS函数消失了

时间:2016-02-08 10:02:14

标签: javascript angularjs session object local-storage

我最初调用此函数时:

var session = function(){
    console.error(service.get());
    if(service.get().session === undefined){
        $localStorage.user.session = {
            timeLeft: 0,  //  Time left until a user session ends (in minutes).
            start: function(timeLeft) {
                $localStorage.user.session.timeLeft = timeLeft;
                $localStorage.user.session.interval;
            },
            stop: function(){
                $interval.cancel($localStorage.user.session.interval);
            },
            interval: $interval(function () {
                $localStorage.user.session.timeLeft -= 1;

                if($localStorage.user.session.timeLeft <= 0){
                    $state.go('signin');
                }
            }, 0.125 * 60000)
        };
    }

    return $localStorage.user.session;
};

function sessionRestart(){
    session();
};
sessionRestart();

它创建了session对象及其所有变量,但是当我重新加载页面时,它不会填充作为函数的变量。我该如何解决这个问题?


修改 该应用程序为AngularJS,我ngStorage使用$localStorage,该代码适用于我的factory中的<Style.Triggers> <DataTrigger Value="True"> <DataTrigger.Binding> <MultiBinding Converter="{StaticResource VisibilityCheckConverter}"> <Binding Path="ProOne" /> <Binding Path="ProTwo" /> <Binding Path="ProThree" /> <Binding Path="ProFour" /> </MultiBinding> </DataTrigger.Binding> <Setter TargetName="Button" Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> 用户会话应用

1 个答案:

答案 0 :(得分:0)

MarcosPérezGude让我想到了问题的解决方案。哪个是移动$interval以外的$localStorage来解决我的问题。不知道为什么它不会起作用。

现在代码如下:

var session,
    startSession: function(timeLeft) {
        if($localStorage.user.session === undefined){
            if(timeLeft === undefined){
                timeLeft = 0;
            }

            $localStorage.user.session = {
                timeLeft: timeLeft
            };
        }

        if (session === undefined) {
            session = $interval(function () {
                $localStorage.user.session.timeLeft -= 1;

                if($localStorage.user.session.timeLeft === undefined){
                    service.stopSession();
                }

                if($localStorage.user.session.timeLeft <= 0){
                    $state.go('signin');
                }
            }, 1 * 60000);  //  Time in milliseconds. Minutes * milliseconds = total milliseconds before the interval repeats.
        }
    },
    stopSession: function() {
        $interval.cancel(session);
        session = undefined;
    };