-1

I am trying to accomplish something like this.

/* Code start */

  function getCurrentTime()
  {
      var date = new Date(),
      time = date.getTime();

      return time;
   }


var jsObject = {
                 "imageOne":"SomeText"+getCurrentTime(),
                 "imageTwo":"someOtherText"+getCurrentTime()
                }


setInterval(function(){
        console.log(jsObject.imageOne);

        }, 3000);
/* Code end */

Each time the timer is executed I need to execute getCurrentTime() and get the latest time. How do I do it?

1
  • Without parens to invoke a function, you need a getter: Code example. Commented Mar 15, 2012 at 18:52

2 Answers 2

2

If you need to execute things and get a different result each time, you basically need a function:

"imageOne": function() {
  return "SomeText" + getCurrentTime();
},

and:

console.log(jsObject.imageOne());

If really necessary, you can omit the () with a getter, but using a function is more straight-forward.


Using a getter:

get imageOne() {
  return "SomeText" + getCurrentTime();
},

This way accessing obj.imageOne will evaluate to a different value each time, depending on the current time.

Sign up to request clarification or add additional context in comments.

6 Comments

Thx pimvdb, I tried function its working fine. I want to know if there is any other way to do this. Because this object has lot of elements(key-value pairs) and is getting populated from some other script.
@phpsessionid: How are they populated? If they are assigned the actual time, e.g. SomeText123456 then it's a bit tricky to replace it with another time.
Actual time is not assigned. I have a big object with lot of links to be send to analytics server. So each time when I hit the analytics server, the url should also have the latest time stamp. Links will be like 50 to 100 numbers.
@phpsessionid: I'm afraid my question still remains: how is the object populated? What values do the properties have? Can't you populate the object with functions such as the one in my answer?
Functions will work fine, I was checking if there is any way to execute the functionCalls in the object each time it is accessed. >>how is the object populated? We are hardcoding the object in the JS file
|
1

Try this:

var jsObject = {
    "imageOne":"SomeText",
    "imageTwo":"someOtherText",
    getLatestByName : function (name) {
       return this[name] + getCurrentTime();
    }
};


setInterval(function(){
    console.log(jsObject.getLatestByName("imageOne"));
}, 3000);

here is the fiddle : http://jsfiddle.net/R7JQ3/

4 Comments

Nemoy, this works, but the time stamp will be inbetween the text. Like imageTwo":"SomeText"+TIME-HERE+"Some other query parameters."
what exactly you want? are you passing the "Some other query parameters" from outside or is it part of your imageTwo property
Nemoy, what I meant is that TIME stamp can be inbetween two string. Thanks for the help, I am going to try your method also.
then use return this[name] + getCurrentTime() + "some other string";

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.