[UPHPU] Javascript callback problem

Jon Jensen jon at jenseng.com
Tue Mar 21 13:10:39 MST 2006


>   The problem is when the callback function is called, it has 
> no context.  For example if you use the code as is, the 
> callback function has no idea what the value of this.name is, 
> but if you uncomment the "//using this line works//" line, 
> and comment out the previous, the code works.  Is there not a 
> way to have a callback reference a function of an object?


One way would be to use an anonymous function instead of timerhandle:

test.prototype.start = function() {
	var me = this;
	this.timer = window.setTimeout(
		function(){
			document.getElementById('log').innerHTML += "Timer
has completed.<br>";
			document.getElementById('log').innerHTML += "Name: "
+ me.name + "<br>";
		}, 2000);
}

You have to use "me" because otherwise "this" has the context of the
anonymous function and not the object.

Jon



More information about the UPHPU mailing list