1

I'm pretty sure I know the answer to this (and that it is no), but is it possible to create a variable that always returns the value of a function without "calling" the variable?

I'm sick of an inactivity warning on a website I use. I look at it a few times a day, but I keep it open in case there's an update I need to look at. It automatically signs me out after 15 minutes using some javascript--the token isn't invalidated by a cookie expiring, nor is my session removed server-side--and the variable it checks is called last_user_action.

I'd like to make last_user_action always point to new Date().getTime();.

last_user_action = function() { new Date().getTime(); }

would work if I could easily change all the references to last_user_action to instead belast_user_action(), but I can't.

last_user_action = (function() { return new Date().getTime(); })();

only sets the value once.

So like I said, I doubt there's a way to do this, but if it is possible, how would I do it?

EDIT It occurs to me now, it'd be easier to just run

window.setInterval(function() { last_user_action = new Date().getTime(); }, 1000 * 60 * 10);

in Chrome's javascript console.

2
  • 4
    setInterval(function(){last_user_action = new Date().getTime();}, 1000) Commented Oct 9, 2014 at 19:14
  • You can use 6e5 instead of 1000 * 60 * 10 Commented Oct 9, 2014 at 19:18

4 Answers 4

4

Well, if you are sure it works that way, you could use setInterval every minute to set the value:

setInterval(function(){
    last_user_action = new Date().getTime();
},60000)
Sign up to request clarification or add additional context in comments.

Comments

2

Not that directly, but it would be trivial to just update the variable periodically.

function updateLastUserAction() {
    last_user_action = new Date().getTime(); // or Date.now();
    setTimeout(updateLastUserAction, 10000); // run again in 10 seconds
}

updateLastUserAction();

You can change the update period to suit you - since the update function is extremely lightweight, you can run it pretty often and it won't matter.

Comments

2

Assuming last_user_action is a global variable, that means it's actually a property of window and you can define a getter for that property.

Object.defineProperty(window, 'last_user_action', {
  get: function() {
    return new Date().getTime();
  }
});
last_user_action; // 1412882205169
last_user_action; // 1412882206490

This would normally be a bad idea, but given you are hacking here, it doesn't seem so bad.

Comments

1

AFAIK you can't do that with a variable.

But you can do it with a property, using a getter:

var obj = {};
Object.defineProperty(obj, 'last_user_action', {
    get: function() { return new Date().getTime(); }
});
obj.last_user_action; // Current date time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.