Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #11708
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news-2.dfn.de!news.dfn.de!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <1350222.i9fspTd0WE@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Sat, 11 Feb 2012 22:06:25 +0100 |
| User-Agent | KNode/4.4.11 |
| Content-Transfer-Encoding | 8Bit |
| 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 |
| MIME-Version | 1.0 |
| Lines | 83 |
| NNTP-Posting-Date | 11 Feb 2012 22:06:26 CET |
| NNTP-Posting-Host | c05fbdac.newsspool2.arcor-online.net |
| X-Trace | DXC=AWS=C7]PFLZPU8j_I0DN6_A9EHlD;3YcR4Fo<]lROoRQ8kF<OcfhCO[IAON^PBnRUWDZm8W4\YJN\b@mF9jNikdUDL6eU71B1ZP1SB1JXVAXdY |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.javascript:11708 |
Followups directed to: comp.lang.javascript
Show key headers only | 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
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