Closed Thread Icon

Topic awaiting preservation: objects, "this" and settimeout() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8474" title="Pages that link to Topic awaiting preservation: objects, &amp;quot;this&amp;quot; and settimeout() (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: objects, &quot;this&quot; and settimeout() <span class="small">(Page 1 of 1)</span>\

 
Scott
Bipolar (III) Inmate

From: schillmania.com
Insane since: Jul 2002

posted posted 01-19-2003 10:04

Recent posts on this stuff reminded me of something I've been wondering about.

Is there a way to reference objects using "this" when using a settimeout()?

ie.

function thing() {
this.i=0;
this.doSomething = function() {
this.i++
window.status=i;
setTimeout("myThing.doSomething()",20); // ideally "this" somehow?
}
}

var t = new Thing();
t.doSomething();

I've used arrays of objects before where each object is passed the index of the array it's in, so it's handy for if you need to reference back to the object from the outside (because of settimeout() or whatever) - but is there a better way around this?

- scott

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-19-2003 19:37

Lotta these questions going around as of late

I'm a little confused though, are you sayign that using setTimeout("this.doSomething()",20); doesn't work?... I'm pretty sure it does... At least I was using "this" within a setInterval() call for an object.function()...

But you do realise that you won't be able to have 2 or more of those doSomething() timeout's running at the same time, regardless of weather or not their in seperate objects. As soon as you trigger another setTimeout() the first one will be stopped. Well, at least they are if you use "this" in the timeout call.

[This message has been edited by Dracusis (edited 01-19-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-19-2003 19:58

Nope the "this" in a setTimeout/setInterval doesn't work. There's no great solution for this, really... if it took a function as its argument rather than a string, it could be done somehow probably... but I don't know of an easy way. I rarely use setTimeouts that extensively.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 01-19-2003 21:10

Scott: I've been puzzled too by the fact that the setTimeout function is only available on the window object. So I throw the following piece of code :

code:
function evalSetTimeout( evalString, delay )
{
return eval( "setTimeout( \""+ evalString.replace( /this/gi, "document.objectsInstanceArray[ "+
this.objectsInstanceArrayIndex +" ]" ) +"\", "+ delay + " )" )
}
function evalSetInterval( evalString, delay )
{
return eval( "etInterval( \""+ evalString.replace( /this/gi, "document.objectsInstanceArray[ "+
this.objectsInstanceArrayIndex +" ]" ) +"\", "+ delay + " )" )
}


function myClass( value )
{
// SHARED INITIALIZATIONS OF THE CLASS
if( !document.objectsInstanceArray )
document.objectsInstanceArray = []
this.objectsInstanceArrayIndex = document.objectsInstanceArray.length
document.objectsInstanceArray[ this.objectsInstanceArrayIndex ] = this
this.setTimeout = evalSetTimeout
this.setInterval = evalSetInterval

// SPECIFIC INITIALIZATION OF THE CLASS
this.value = value

this.doSomething = myClass_doSomething
function myClass_doSomething( iteration )
{
document.body.innerHTML += "<hr>object #"+ this.objectsInstanceArrayIndex +"<br>value = "+
this.value +"<br>iteration # "+ iteration
if( --iteration>0 )
this.setTimeout( "this.doSomething( "+ iteration +" )", 500 )
}
}

myClassInstance1 = new myClass( "@___@" )
myClassInstance2 = new myClass( "zZZZZzz!" )

myClassInstance1.doSomething( 3 )
myClassInstance2.doSomething( 4 )
myClassInstance2.setTimeout( "alert( this.value )", 50 )


This little hack stacks the reference of the objects in a global array, overloads the setTimeout and setInterval by a function that evaluates a string in which the keyword this is replaced by the reference in the global array. I did that quickly so the parameters of the setTimeout & setInterval methods aren't well parsed

Indeed, having the setTimeout and setInterval methods available on any instances of your classes can be really handy.

Cheers,

Mathieu "POÏ" HENRI

[Emp edit: Killed HSBoD]

[This message has been edited by Emperor (edited 06-26-2003).]

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 01-19-2003 23:30

I've been using the setTimeout within objects for quite a while now and I had always made the object name a member of the object that needed a setTimeout. Then I just called the timer using that. I have looked for alternatives for this and have not found any.

. . : slicePuzzle

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 01-20-2003 01:06

I got a line from bratta a long time ago

function thing() {
this.i=0;
// Go these lines from bratta a long time ago.
this.obj = 'Object';
eval (this.obj +' =this');
this.doSomething = function() {
this.i++;
if (this.i <= 20) window.status = this.i;
setTimeout(this.obj+".doSomething()",20);
}
return this;
}



.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-20-2003 01:49

Clever. With that, you only need to find a separate ID string for each object.

« BackwardsOnwards »

Show Forum Drop Down Menu