setTimout error

V

Vincent M.

Hello,

I call a function like that:
check_img(vti, img) ;
And this call works, the function check_img works, so all is all right.

But I have to call it with a setTimeoutSauf, I tried this:
J'ai essayé ceci:
setTimeout("check_img(" + vti + ", '" + img + "')", 20) ;

But it does not work, IE outputs an error message and Mozilla outputs :
Missing ] after element list

I don't know how to write it correctly.


A little far away I have another setTimeout error to call the same function:
setTimeout("check_imgloaded(" + var_img + ", '" + img_id + "')", 20) ;

But this time var_img is a variable (object), Image object and 'img_i'
is a string variable but it outputs the same error message !!!
A l'aide

Thanks,
Vincent.

PS: I think there is something wrong with [ ]
 
M

Michael Winter

On Mon, 05 Apr 2004 16:47:00 +0200, G Roydor

[fixed top-post]
Vincent M. a écrit:

[snipped first call]

When you concatenate an object to a string, the object's toString() method
is called. This produces a string along the lines of

[object <objectName>]

where <objectName> depends on the object type and browser executing the
code. When setTimeout tried to execute the function, it will see

check_img( [object someName], 'some string' )

As you can see, this is clearly a syntax error.

This should be an adequate solution

function timer( func, ms, param1, param2 ) {
setTimeout( function() {
func( param1, param2 );
}, ms );
}

After placing this once in a script block or file, you would replace
(using an example from your original post)

setTimeout("check_img(" + vti + ", '" + img + "')", 20) ;

with

timer( check_img, 20, vti, img );

Unfortunately, this won't work with the IE4 and NN4 generation of browsers
as they don't allow function references as arguments to the setTimeout()
method.
Essayez setTimeout(check_img(vti, img), 20) ;


That will result in an immediate call to check_img(), which is not what
the OP wants.

To the OP: why are you calling a function after twenty milliseconds? What
do you expect to happen in that time which means you can't execute the
call immediately?

Mike
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top