🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chile.comp.programacion
Article #2

Javascript: setTimeout() dentro de un objeto

#2
From: Marious
Date: Tue, 04 Aug 2009 21:30
46 lines
2430 bytes
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 si a alguien se le ocurre y me puede ayudar...
Thx~
Technical Details
Message-ID: <h5a9b5$is5$1@news.nivel5.cl>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 5075a5d0e4,6a8c59e449cbbab,start
X-Google-Attributes: gid5075a5d0e4,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: SPANISH,UTF8
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!not-for-mail
From: Marious <sinspam@tampocoexiste.ivn.cl>
Newsgroups: chile.comp.programacion,chile.comp.internet,chile.comp.www
Subject: Javascript: setTimeout() dentro de un objeto
Date: Tue, 04 Aug 2009 17:30:27 -0400
Organization: Varios
Lines: 46
Message-ID: <h5a9b5$is5$1@news.nivel5.cl>
NNTP-Posting-Host: nat.stockergroup.com
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.nivel5.cl 1249421477 19333 200.73.16.136 (4 Aug 2009 21:31:17 GMT)
X-Complaints-To: news@news.nivel5.cl
NNTP-Posting-Date: Tue, 4 Aug 2009 21:31:17 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1b3pre) Gecko/20090513 Fedora/3.0-2.3.beta2.fc11