1
function Timer() {
 this.initialTime = 0;
 this.timeStart = null;

 this.getTotalTime = function() {
  timeEnd = new Date();
  diff = timeEnd.getTime() - this.timeStart.getTime();

  return diff+this.initialTime;
 };

 this.formatTime = function() {
  interval = new Date(this.getTotalTime());

  return  interval.getHours() + ":" +  interval.getMinutes() + ":" + interval.getSeconds();
 };

 this.start = function() {
  this.timeStart = new Date();

  setTimeout("this.updateTime()", 1000);
 };

 this.updateTime = function() {
  alert(this.formatTime());
  setTimeout("this.updateTime()", 1000);
 };
}


timer = new Timer();
timer.start();

I am getting an error:

this.updateTime is not a function

Any ideas?

Thanks

2
  • I wonder if we can/should change this question to be a very generic one. All the code in the question didn't help me, I just went straight to the answer :). Commented Oct 13, 2011 at 14:49
  • Turns out, setTimeout with a string as its first argument behaves like an indirect eval. Commented Sep 30, 2021 at 3:37

4 Answers 4

2

Your string is not evaluated in the context of your object, so this doesn't refer to what you think it does.

You should not be passing a string argument to setTimeout. Instead, you should pass an anonymous function that calls your method with a saved copy of this.

For example:

var self = this;
setTimeout(function() { self.updateTime(); }, 1000);

The self variable is necessary because setTimeout's callback is also not evaluated in the context of your object.

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

7 Comments

this works but.. is there a more elegant way? Without defining the global variable?
That's a local, not a global. You could use a proxy function.
Ok.. I'm not a JS expert by any means. Thanks!
self is a regular local variable. Because the anonymous function is in the same scope, it saves references to all the local variables. This is an extremely powerful feature called a closure.
Actually, if you define self inside the updateTime function, it's not guaranteed to refer to the Timer object either. There is no difference between your code and a direct setTimeout(this.updateTime). this/self have the same context. :)
|
1

try

var me = this;
setTimeout(function() { me.updateTime() }, 1000);

Comments

1

is there a more elegant way?

Yes, in ECMAScript Fifth Edition:

setTimeout(this.updateTime.bind(this), 1000);

However, until all browsers support Fifth Edition (which they don't yet by a long measure), you should add your own implementation of Function.bind as fallback. eg.:

// Add ECMA262-5 method binding if not supported natively
//
if (!('bind' in Function.prototype)) {
    Function.prototype.bind= function(owner) {
        var that= this;
        if (arguments.length<=1) {
            return function() {
                return that.apply(owner, arguments);
            };
        } else {
            var args= Array.prototype.slice.call(arguments, 1);
            return function() {
                return that.apply(owner, arguments.length===0? args : args.concat(Array.prototype.slice.call(arguments)));
            };
        }
    };
}

You might also prefer to use setInterval to avoid the repeated setTimeout calls. Finally, you should remember to declare all your variables (timeEnd, diff, etc.) as var, otherwise you're getting accidental globals, which can cause you horrible debugging heartache.

Comments

0

If you supply a string to setTimeout, this string will be executed literally. The thing is, it will be executed sometime later in the global context, outside your Timer object, so this means something completely different.

Just pass the function itself like so:

function Timer() {
  var self = this;   // make reference to this that won't change with context

  this.updateTime = function() {
    alert(self.formatTime());
    setTimeout(self.updateTime, 1000);
  };

}

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.