Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setTimeout on privileged methods

Status
Not open for further replies.

Erakis

Programmer
Jan 10, 2003
26
CA
Hi,

I'm having problem when calling privileged method from setTimeout. First call is well fired but the others failed, someone know why ?

Code:
function MyObject( x, y, z ) {
	this.StartMovement = function() {
		// ...
		DoMove();
		// ...
	}
	
	function DoMove() {
		alert( "Moving..." );
		setTimeout( "DoMove()", 1000 );
	}
}

// Test call
var Test = new MyObject( 0, 0, 0 );
Test.StartMovement();

Thanks for your help.
 
Because the DoMove function is inside MyObject, the setTimeout call would have to refer to the object. In your case, you'd need to investigate the "call" method of the function object:


MSDN said:
The call method is used to call a method on behalf of another object. The call method allows you to change the object context of a function from the original context to the new object specified

so in your case, the setTimeout call might be something like:

Code:
DoMove.call(MyObject);

If you're into using the Prototype JS framework, you could use the "bind" function in much the same way.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi,

I don't know if I understood but the call might me something like :

Code:
setTimeout( "DoMove.call(MyObject)", 1000 );

Thanks
 
I tried :

Code:
setTimeout( "DoMove.call(MyObject)", 1000 );

I'm still getting the following error : Error: 'DoMove is not defined'
 
Hi,

You said :
Code:
setTimeout( "MyObject.call(Test);", 1000 );

But where is the "DoMove()" call method in that ;) ?
 
Blimey - I must be having an off day today :)

Your problem is that "DoMove" is not a privileged method, but a private one. To make it privileged, you need to change the function declaration and code to :

Code:
this.DoMove = function() {
	var _this = this;
	setTimeout(function() { _this.DoMove(); }, 1000 );
}

These two articles helped in the solving of this one:



Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok.

Thanks for issues about privileged function, but I did a mistake in my question. The function MUST be PRIVATE.

So how do I call this method if it is PRIVATE ?

Thanks
 
Er - you can't. Why do you think private functions are called "private"?

Hint: If you don't know, read the first of the two links I posted at the bottom of my last post.

Note: Why does it have to be a private method? Why not make it public / privileged?

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi,

I'm a C++ (MFC)/C# programmer so you don't worry, I know what is a private or public method. What I show you there is simply a short exemple to explain you the problem. I'm developping an animation script and some method has to be privileged but this one has to be PRIVATE.

I try to pass the method as REFERENCE :
Code:
setTimeout( DoMove, 1000 );

and it is working ! :)
But now, could you tell me WHY it is JUST working when passing by reference ?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top