Article View: chile.comp.programacion
Article #5Re: Javascript: setTimeout() dentro de un objeto
From: Jos� Miguel Santib��ez
Date: Tue, 04 Aug 2009 23:08
Date: Tue, 04 Aug 2009 23:08
83 lines
4061 bytes
4061 bytes
En el cap�tulo anterior (news:h5a9b5$is5$1@news.nivel5.cl), Marious
<sinspam@tampocoexiste.ivn.cl> dijo:
> Tengo un objeto con dos funciones, una que crea un div, y
> otra que le aumenta el alto de 5 en 5 hasta llegar a 20
> (osea 4 veces).-
>
> objeto =
> {
> sumar: 5,
> pasos: 4,
> crearDiv: function()
> {
> this.elDiv = document.createElement('div');
> // aquí se pone cualquier codigo para
> // darle estilos y añadirlo a algún otro elemento
> },
> agrandarDiv: function()
> {
> if (!this.tamanno) this.tamanno = 0;
> this.pasos -= 4;
> this.tamanno += this.sumar;
> this.elDiv.style.height = this.tamanno+'px';
> if (this.pasos != 0) setTimeout(this.agrandarDiv,100);
> // Ahí está el drama ↑
> }
> }
>
> Si llamo a la función de agrandarDiv inmediatamente, funciona a la
> perfección... no así con un “setTimeout”.
>
> Necesito usar setTimeout porque quiero una demora de 100ms entre cada
> suma para que así se vea aparecer el div.
>
> El problema es que al hacer un setTimeout, la función no se llamará
> en el mismo contexto, sino fuera, y “this” perderá su valor
> original... osea, la referencia al objeto de la función.
>
> Lo que busco, es una solución para poder realizarlo,
> pero con las siguientes reglas.
>
> No utilizar el nombre estatico del objeto.
> No utilizar ID para referirse al div.
> No registrar una variable global que haga referencia al objeto.
> Que me permita cambiar el nombre del objeto y no haya que
> tocar el codigo de este.
>
A ver, no hay una soluci�n que responda bien a todos tus requerimientos.
Simplemente, porque la funci�n SetTimeOut no es una funci�n "orientada a
objetos".
Lo m�s cercano a lo que quieres, (y que se amarra de un peque�o resquicio
legal que dejaste abiero) es utilizar una variable global, que sea un
"arreglo" a los objetos. Algo as� como:
var misobjetos = new Array();
objeto =
{
sumar: 5,
pasos: 4,
crearDiv: function()
{
this.indice = misobjetos.length;
misobjetos[this.indice] = this;
this.elDiv = document.createElement('div');
// aquí se pone cualquier codigo para
// darle estilos y añadirlo a algún otro elemento
},
agrandarDiv: function()
{
if (!this.tamanno) this.tamanno = 0;
this.pasos -= 4;
this.tamanno += this.sumar;
this.elDiv.style.height = this.tamanno+'px';
if (this.pasos != 0)
setTimeout(misobjetos[this.indice].agrandarDiv, 100);
}
}
--
Jose M. Santiba�ez A.
<jms@nivel5.cl>
<http://caos.cl/jms>
Technical Details
Message-ID:
<Xns9C5DC2A5539ADjmsnews@200.54.144.211>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.tie.cl!not-for-mailReferences:
<h5a9b5$is5$1@news.nivel5.cl>Show Full Headers
Complete Article Headers
X-Google-Thread: 5075a5d0e4,6a8c59e449cbbab
X-Google-Attributes: gid5075a5d0e4,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.tie.cl!not-for-mail
From: "Jos� Miguel Santib��ez" <jms@nivel5.cl>
Newsgroups: chile.comp.programacion,chile.comp.internet,chile.comp.www
Subject: Re: Javascript: setTimeout() dentro de un objeto
Date: Tue, 4 Aug 2009 23:08:06 +0000 (UTC)
Organization: Caos Consultores
Lines: 83
Message-ID: <Xns9C5DC2A5539ADjmsnews@200.54.144.211>
References: <h5a9b5$is5$1@news.nivel5.cl>
NNTP-Posting-Host: 201-236-160-163.adsl.tie.cl
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: jagger.tie.cl 1249427286 27030 201.236.160.163 (4 Aug 2009 23:08:06 GMT)
X-Complaints-To: news@jagger.tie.cl
NNTP-Posting-Date: Tue, 4 Aug 2009 23:08:06 +0000 (UTC)
User-Agent: Xnews/5.04.25
X-Face: +|NgjgFM{<rs%\Q|vUu16]a6,}?MJTA<!PEEI+x&Gw-Gyq+?$}fvo+.(,-r;)~Qzn)#{{nxU,OT?w^s<7m4ckCJ*L$c#I)yHL7b)GdU*#l