javascript - JS functions in object disappears -
when call function:
var session = function(){ console.error(service.get()); if(service.get().session === undefined){ $localstorage.user.session = { timeleft: 0, // time left until 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; };
from
function sessionrestart(){ session(); }; sessionrestart();
it creates session
object variables, when reload page doesn't populate variables functions. how can fix that?
edit application angularjs
, i'm using ngstorage
$localstorage
, , code meant user session can found in factory
in application.
marcospƩrezgude made me think of solution problem. move $interval
outside of $localstorage
fixed problem. not sure why wouldn't work thou.
the code looks this:
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 interval repeats. } }, stopsession: function() { $interval.cancel(session); session = undefined; };
Comments
Post a Comment