Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #11708

Re: Simple parameter passing within a setInternal() call?

Message-ID <1350222.i9fspTd0WE@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-02-11 22:06 +0100
Subject Re: Simple parameter passing within a setInternal() call?
Newsgroups comp.lang.javascript
References <jh6e3o$73s$1@news.albasani.net> <jh6jln$jjo$1@news.albasani.net>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Andreas Bergmaier wrote:

> Tuxedo schrieb:
>> How can a simple string parameter be passed between two functions while
>> inside a setInterval call? For example:
>>
>> function initialFunction(x){
>> alert(x);
>> setInterval('repeatingStuff(x)',5000);
>> }
>>
>> function repeatingStuff(x){
>> alert(x);
>> }
>> ...
>>
>> <a href="..." onclick="initialFunction('param_x');">stuff</a>
>>
>> But the error "x is not defined" results from ..Stuff(x) in
>> setInterval('repeatingStuff(x)',5000). How can the parameter between the
>> two functions be passed within the setInteval bit?
> 
> x must be in the scope of the second function.

In the scope _chain_.

> Therefore, you should define repeatingStuff inside the initialFunction:
> 
> function initialFunction(x){
>     alert(x);            ^
            ^--------------'
>     function repeatingStuff(x){
                              ^
>        alert(x);            |
               ^--------------'
>     }
>     setInterval(repeatingStuff,5000);
> }
> 
> or shorter:
> 
> function initialFunction(x){
>     alert(x);            ^
            ^--------------'
>     setInterval( function(x){
>        alert(x);          ^
               ^------------'
>     }, 5000);
> }

This does not work, because in the context of the function (referred by 
`repeatingStuff'), `x' is bound to the inner function's argument `x' (which 
is not passed a useful value from the DOM implementation anyway) and not to 
that of the inner function's definition context.  Please test before you 
post, or clearly mark code as untested.

> You should pass a function to setIntervall/Timeout, instead of a code to
> be evaluated each time (slow & buggy).

The function code also will have to be evaluated each time.  But it is true 
that evaluating the string value as a Program is less efficient and more 
error-prone than that.
 
> Another alternative is to use bind() or other functions, to return a
> function which calls repeatingStuff with the needed parameters:
> 
> function repeatingStuff(){...}
> function initialFunction(x){
>     alert(x);
>     setInterval( repeatingStuff.bind(this, x), 5000);
> }

This works in implementations of ECMAScript 5.x or where you have a suitable 
scripting framework that emulates Function.prototype.bind(), but it is 
unnecessarily inefficient by comparison.


PointedEars
-- 
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
  -- Richard Cornford, comp.lang.javascript, 2011-11-14

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Simple parameter passing within a setInternal() call? Tuxedo <tuxedo@mailinator.com> - 2012-02-11 20:04 +0100
  Re: Simple parameter passing within a setInterval() call? Tuxedo <tuxedo@mailinator.com> - 2012-02-11 20:39 +0100
    Re: Simple parameter passing within a setInterval() call? Dr J R Stockton <reply1206@merlyn.demon.co.uk> - 2012-02-12 22:02 +0000
  Re: Simple parameter passing within a setInternal() call? Andreas Bergmaier <andber93@web.de> - 2012-02-11 21:39 +0100
    Re: Simple parameter passing within a setInternal() call? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-11 22:06 +0100
      Re: Simple parameter passing within a setInternal() call? Andreas Bergmaier <andber93@web.de> - 2012-02-11 22:33 +0100
        Re: Simple parameter passing within a setInternal() call? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-11 23:44 +0100
          Re: Simple parameter passing within a setInternal() call? Andreas Bergmaier <andber93@web.de> - 2012-02-12 03:04 +0100
        Re: Simple parameter passing within a setInterval() call? Tuxedo <tuxedo@mailinator.com> - 2012-02-12 09:22 +0100

csiph-web