Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #11707
| From | Andreas Bergmaier <andber93@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Simple parameter passing within a setInternal() call? |
| Date | 2012-02-11 21:39 +0100 |
| Organization | albasani.net |
| Message-ID | <jh6jln$jjo$1@news.albasani.net> (permalink) |
| References | <jh6e3o$73s$1@news.albasani.net> |
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. 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);
}
You should pass a function to setIntervall/Timeout, instead of a code to
be evaluated each time (slow & buggy).
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);
}
Greetings,
Bergi
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