Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #11705 > unrolled thread
| Started by | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| First post | 2012-02-11 20:04 +0100 |
| Last post | 2012-02-12 09:22 +0100 |
| Articles | 9 — 4 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| Date | 2012-02-11 20:04 +0100 |
| Subject | Simple parameter passing within a setInternal() call? |
| Message-ID | <jh6e3o$73s$1@news.albasani.net> |
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?
Many thanks,
Tuxedo
[toc] | [next] | [standalone]
| From | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| Date | 2012-02-11 20:39 +0100 |
| Subject | Re: Simple parameter passing within a setInterval() call? |
| Message-ID | <jh6g5k$bfn$1@news.albasani.net> |
| In reply to | #11705 |
Tuxedo wrote:
[...]
> setInterval('repeatingStuff(x)',5000). How can the parameter between the
I thought I had tried this already but I must have simply got the quoting
wrong. Of course, this works:
setInterval('repeatingStuff("' + x + '")',5000);
Tuxedo
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1206@merlyn.demon.co.uk> |
|---|---|
| Date | 2012-02-12 22:02 +0000 |
| Subject | Re: Simple parameter passing within a setInterval() call? |
| Message-ID | <wU5jsAUlbDOPFwgx@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #11706 |
In comp.lang.javascript message <jh6g5k$bfn$1@news.albasani.net>, Sat,
11 Feb 2012 20:39:32, Tuxedo <tuxedo@mailinator.com> posted:
>Tuxedo wrote:
>
>[...]
>
>> setInterval('repeatingStuff(x)',5000). How can the parameter between the
>
>I thought I had tried this already but I must have simply got the quoting
>wrong. Of course, this works:
>setInterval('repeatingStuff("' + x + '")',5000);
That will use the value of x at call time, if x is simple or an
expression.
Another way is to pass x as a global variable; or, perhaps more clearly,
as a property of a global Object reserved and named, or otherwise
suitable, for the purpose. Actually, repeatingStuff is such an Object.
That eliminates conversions to/from String, which may be inexact.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
[toc] | [prev] | [next] | [standalone]
| From | Andreas Bergmaier <andber93@web.de> |
|---|---|
| Date | 2012-02-11 21:39 +0100 |
| Message-ID | <jh6jln$jjo$1@news.albasani.net> |
| In reply to | #11705 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-02-11 22:06 +0100 |
| Message-ID | <1350222.i9fspTd0WE@PointedEars.de> |
| In reply to | #11707 |
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
[toc] | [prev] | [next] | [standalone]
| From | Andreas Bergmaier <andber93@web.de> |
|---|---|
| Date | 2012-02-11 22:33 +0100 |
| Message-ID | <jh6mr2$rbb$1@news.albasani.net> |
| In reply to | #11708 |
Thomas 'PointedEars' Lahn schrieb:
> Andreas Bergmaier wrote:
>> Tuxedo schrieb:
>>> How can a simple string parameter be passed between two functions while
>>> inside a setInterval call?
>>
>> 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.
I'm sorry, I forgot the essential thing when copying the example code :-(
@Tuxedo: The thing is to have only one variable "x". The code should be
function initialFunction(x){
alert(x);
setInterval( function(){
alert(x);
}, 5000);
}
>> Another alternative is to use bind() or other functions, to return a
>> function which calls repeatingStuff with the needed parameters
>
> 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.
Is it? Unnecessarily in this case, yes, but if you have defined
"repeatingStuff" elsewhere it is imho much more elegant than coding
var x;
setInterval( function(){
doRepeatingStuffWith(x);
}, 5000);
Bergi
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2012-02-11 23:44 +0100 |
| Message-ID | <8011104.oiZZxVaoWa@PointedEars.de> |
| In reply to | #11709 |
Andreas Bergmaier wrote:
> Thomas 'PointedEars' Lahn schrieb:
>> Andreas Bergmaier wrote:
>>> Another alternative is to use bind() or other functions, to return a
>>> function which calls repeatingStuff with the needed parameters
>>
>> 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.
>
> Is it? Unnecessarily in this case, yes, but if you have defined
> "repeatingStuff" elsewhere it is imho much more elegant than coding
>
> var x;
> setInterval( function(){
> doRepeatingStuffWith(x);
> }, 5000);
That is not even equivalent in the first place. Clearly you do not
understand bind(). Please refrain from recommending it until you do.
PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
[toc] | [prev] | [next] | [standalone]
| From | Andreas Bergmaier <andber93@web.de> |
|---|---|
| Date | 2012-02-12 03:04 +0100 |
| Message-ID | <jh76n4$7ih$1@news.albasani.net> |
| In reply to | #11710 |
Thomas 'PointedEars' Lahn schrieb:
> Andreas Bergmaier wrote:
>> Thomas 'PointedEars' Lahn schrieb:
>>> Andreas Bergmaier wrote:
>>>> Another alternative is to use bind() or other functions, to return a
>>>> function which calls repeatingStuff with the needed parameters
>>>
>>> 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.
>>
>> Is it? Unnecessarily in this case, yes, but if you have defined
>> "repeatingStuff" elsewhere it is imho much more elegant than coding
>>
>> var x;
>> setInterval( function(){
>> doRepeatingStuffWith(x);
>> }, 5000);
>
> That is not even equivalent in the first place. Clearly you do not
> understand bind(). Please refrain from recommending it until you do.
I do, and I never said it was to be equivalent. Of course bind() needs a
context to set, dereferences x, and builds a new function. That even
could be useful (I'm not sure what the OP needs), so why shouldn't I
mention it as an alternative?
Bergi
[toc] | [prev] | [next] | [standalone]
| From | Tuxedo <tuxedo@mailinator.com> |
|---|---|
| Date | 2012-02-12 09:22 +0100 |
| Subject | Re: Simple parameter passing within a setInterval() call? |
| Message-ID | <jh7ssk$itp$1@news.albasani.net> |
| In reply to | #11709 |
Andreas Bergmaier wrote:
[...]
> @Tuxedo: The thing is to have only one variable "x". The code should be
>
> function initialFunction(x){
> alert(x);
> setInterval( function(){
> alert(x);
> }, 5000);
> }
Thanks - works great! Much better than my quoted parameters as in
Stuff("' + x + '")',5000 etc.
Tuxedo
[..]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web