Wednesday, November 4, 2009

[jQuery] Re: setTimeout with variables (in a jQuery statement)

Thanks a lot for you answer, Michel.

I have already switched to using a function within setTimeout, but I
found my problem lies elsewhere. I might open a new thread with a
better fitting title for this.

On 4 Nov., 14:06, Michel Belleville <michel.bellevi...@gmail.com>
wrote:
> I'd use an anonymous function and pass it to setTimeout like this :
>
> var selector = '#whatever .selector';
> var savVal = 'whatever value';
> setTimeout(function() {
> $(selector).val(savVal);
>
> }, 1000);
>
> Notes for the careful reader :
>
>    - There no "this" in the anonymous function, why is that ?
>       - Because in setTimeout this would refers to window which is the owner
>       of setTimeout.
>    - Why are the selector and savVal variables declared using var just
>    before we setTimeout ?
>       - Because I don't want to risk crossing whatever variables may hold
>       the same name in another context thus risking them being
> overwritten by some
>       other script, but I want them to be part of the anonymous
> function's context
>       when it is called.
>
> Hope it helps.
>
> Michel Belleville
>
> 2009/11/4 north <ollo...@web.de>
>
> > Hi,
>
> > I'm trying to use a jQuery statement inside a setTimeout function, but
> > I don't get it to work. I tried a lot of variants, like this one (I'm
> > using 'this' because the setTimeout is inside an each function, and
> > the selector is cached/stored in an object, thus the $selector):
>
> > setTimeout("" + this.$selector + ".val('" + this.savVal + "')", 1);
>
> > How do I have to write this?
>
> > Thanks!

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate