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> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn 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: 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 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); >> } >> ... >> >> stuff >> >> 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