Article View: chile.comp.programacion
Article #4Re: Javascript: setTimeout() dentro de un objeto
From: Marious
Date: Tue, 04 Aug 2009 22:45
Date: Tue, 04 Aug 2009 22:45
56 lines
2944 bytes
2944 bytes
On 08/04/2009 05:34 PM, Benjamin Porter wrote:
> "Marious"<sinspam@tampocoexiste.ivn.cl> escribió en el mensaje
> news:h5a9b5$is5$1@news.nivel5.cl...
>> 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~
>
>
> ¿window.setTimeout?
>
>
¿?
Technical Details
Message-ID:
<h5adna$jaa$1@news.nivel5.cl>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.glorb.com!news2.glorb.com!news.alt.net!news.kjsl.com!news.nivel5.cl!not-for-mailReferences:
<h5a9b5$is5$1@news.nivel5.cl> <h5a9ga$p0u$1@jagger.tie.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,UTF8 Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news.glorb.com!news2.glorb.com!news.alt.net!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: Re: Javascript: setTimeout() dentro de un objeto Date: Tue, 04 Aug 2009 18:45:12 -0400 Organization: Varios Lines: 56 Message-ID: <h5adna$jaa$1@news.nivel5.cl> References: <h5a9b5$is5$1@news.nivel5.cl> <h5a9ga$p0u$1@jagger.tie.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 1249425962 19786 200.73.16.136 (4 Aug 2009 22:46:02 GMT) X-Complaints-To: news@news.nivel5.cl NNTP-Posting-Date: Tue, 4 Aug 2009 22:46:02 +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 In-Reply-To: <h5a9ga$p0u$1@jagger.tie.cl>