missing ] after element list

Z

zephyr

I get this error
"missing ] after element list"
on the line containing the var t=setTimeout("...

args={
parentEl:args.parentEl,
currWidth: currWidth,
timeout: args.timeout,
orgWidth:args.orgWidth,
callback:args.callback,
timeStep:timeStep
};
var t=setTimeout("createTimeBar("+args+")",refresh);

I need to call this function "createTimeBar()" with the argument args
but the string concatenation (+) cripples the object argument. I don't
know of any other way to use settimeout with a object argument...

Whats the fix here?

Marc
 
S

Scott Sauyet

zephyr said:
var t=setTimeout("createTimeBar("+args+")",refresh);

I need to call this function "createTimeBar()" with the argument args
but the string concatenation (+) cripples the object argument. I don't
know of any other way to use settimeout with a object argument...

var t = setTimeout(function() {createTimeBar(args);}, refresh);

setTimeout does not have to take a String. And it's a very rare case
where doing so makes real sense. Instead, just pass in a function.

Cheers,

-- Scott
 
G

Gregor Kofler

Am 2011-04-06 23:28, zephyr meinte:
I get this error
"missing ] after element list"
on the line containing the var t=setTimeout("...

args={ ^^^^
parentEl:args.parentEl,
^^^^
currWidth: currWidth,
timeout: args.timeout,
orgWidth:args.orgWidth,
callback:args.callback,
timeStep:timeStep
};

I doubt that the above is intended. (And make it var args = ...)
var t=setTimeout("createTimeBar("+args+")",refresh);
I need to call this function "createTimeBar()" with the argument args
but the string concatenation (+) cripples the object argument.

It doesn't cripple it; it's the object's toString() method which is
called upon string concatenation: args.toString() gives [object Object]
I don't
know of any other way to use settimeout with a object argument...

What's wrong with

var t=setTimeout(createTimeBar(args),refresh);

(Support for functions instead of code to evaluate is supported since
JS1.2/MSHTML DOM 5.0...)


Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
zephyr meinte:
var t=setTimeout("createTimeBar("+args+")",refresh);

I need to call this function "createTimeBar()" with the argument args
but the string concatenation (+) cripples the object argument.

It doesn't cripple it; it's the object's toString() method which is
called upon string concatenation: args.toString() gives [object Object]
I don't know of any other way to use settimeout with a object argument...

What's wrong with

var t=setTimeout(createTimeBar(args),refresh);

It is not equivalent, as it will call createTimeBar(args) when setTimeout()
is called, not `refresh' milliseconds after setTimeout() returned. Most
certainly you mean instead:

var t = window.setTimeout(function() { createTimeBar(args); }, refresh);

Assuming that this is not part of a loop with changing `args', in which case
another closure would be required.
(Support for functions instead of code to evaluate is supported since
JS1.2/MSHTML DOM 5.0...)

But createTimeBar() does not return (a reference to) one, so …


PointedEars
 
G

Gregor Kofler

Am 2011-04-07 08:14, Thomas 'PointedEars' Lahn meinte:
Gregor said:
zephyr meinte:
var t=setTimeout("createTimeBar("+args+")",refresh);

I need to call this function "createTimeBar()" with the argument args
but the string concatenation (+) cripples the object argument.

It doesn't cripple it; it's the object's toString() method which is
called upon string concatenation: args.toString() gives [object Object]
I don't know of any other way to use settimeout with a object argument...

What's wrong with

var t=setTimeout(createTimeBar(args),refresh);

It is not equivalent, as it will call createTimeBar(args) when setTimeout()
is called, not `refresh' milliseconds after setTimeout() returned. Most
certainly you mean instead:

var t = window.setTimeout(function() { createTimeBar(args); }, refresh);

Indeed. I should refrain from posting that late.
Assuming that this is not part of a loop with changing `args', in which case
another closure would be required.


But createTimeBar() does not return (a reference to) one, so …

(It could - the return value of createTimeBar() is unknown. But as you
noted, these are two different stories.)

Gregor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top