animated gif stops when doing cpu intensive task

S

samuelberthelot

Hi,
I have the following code that shows up a div with an animated gif
inside, executes a cpu intensive function using setTimeout and then
hide the div again:

getEl('divWaiting').style.display = 'block';
window.setTimeout(function(){me.FM.addFlagPopBox(me);
getEl('divWaiting').style.display = 'none';}, 1000);

issue is that the animated gif will stop as soon as the function in
setTimeout starts. How can I solve this ?

Thanks

ps: me is a global variable that refers to this object.
 
E

Erwin Moller

Hi,
I have the following code that shows up a div with an animated gif
inside, executes a cpu intensive function using setTimeout and then
hide the div again:

getEl('divWaiting').style.display = 'block';
window.setTimeout(function(){me.FM.addFlagPopBox(me);

I think you made a triple mistake here:
window.setTimeout(function(){me.FM.addFlagPopBox(me);

1) it is missing the }
2) it is also missing the number of milliseconds before executing the
function.
3) The first argument should be a string to be executed, not a reference to
an anonymous function.

So try something like:
setTimeout("FM.addFlagPopBox(me);",100);

where you are responsible to make a function with that name of course that
handles whatever it is you are trying to accomplish.

Didn't you get an errormessage on that line?
If so, you are probably using IE for development, which is a poor choiche
compared to Firefox and the likes. The latter has a javascriptconsole which
will give you detailed errormessages.

Hope that helps.
Regards,
Erwin Moller
 
S

samuelberthelot

Hi Erwin,
Sorry I completely messed up when I pasted my code in the forum :(
My code is correct and I have no errors:

getEl('divExpandedFlags').style.display = 'block';
this.CloseButton =
getChildElement(getChildElement(getEl('divExpandedFlags'),
'divExpandedFlagsHeader'), 'divExpandedFlagsClose');

getEl is a function that do the same as getElementById.

Anyway, the issue is in my first post.

Sam
 
S

samuelberthelot

argggggg what am i doing, i pasted wrong code again. The code I pasted
in the first post was correct, it was split up on two lines by the
editor but it should be read as one line, look again.
 
E

Erwin Moller

argggggg what am i doing, i pasted wrong code again. The code I pasted
in the first post was correct, it was split up on two lines by the
editor but it should be read as one line, look again.

Hi,

Nevermind, if you live in Europe you are probably desintegrating because of
the heat just like me. ;-)

Still, here is your original code:

getEl('divWaiting').style.display = 'block';
window.setTimeout(function(){me.FM.addFlagPopBox(me);
getEl('divWaiting').style.display = 'none';}, 1000);

This indeed solves my comment 1 and 2, but 3 still stands:
"The first argument should be a string to be executed, not a reference to an
anonymous function."

You are still not passing a string to setTimeout.
You should call it like this: setTimeout("STRING HERE", 1000);

And again: Why didn't you see an error?

So this has little to do with high CPU load, but with an invalid call to
setTimeout().

I am unsure if you can pass an Objectreferences to a setTimeout function.
I always avoid such situations anyway by simply calling a function with a
string-parameter, or an integer, that handles the whole shebang.
Like this:
setTimeout("doFlagPopBoxStuff('me');", 1000);

and in doFlagPopBoxStuff you make sure you make a valid reference to your
me-object. Then do your stuff with it.

The reason I warn you about this is simply that when the 1000 millies pass,
your reference can be invalid (eg a removed form-element).

Hope that helps,

Regards,
Erwin Moller
 
R

Richard Cornford

Erwin Moller wrote:
This indeed solves my comment 1 and 2, but 3 still stands:
"The first argument should be a string to be executed, not a reference
to an anonymous function."

This is not true, at least from IE 4, Netscape 4, opera 6 and many
others. A function reference may be the first argument to setTimeout,
Indeed it can be argued that a function reference is a better argument
to use, partly because it can be executed faster (not needing
interpreting into code) and partly because a string object can be
provide with a custom toString method that would return a string that
effectively called the function, providing optimum performance plus
full backward compatibility with older setTimeout implementations that
only accept string arguments.
You are still not passing a string to setTimeout.
You should call it like this: setTimeout("STRING HERE", 1000);

And again: Why didn't you see an error?
<snip>

Even on an implementation that only accepts string arguments the effect
of passing a function reference would be expected to be implicit
type-conversion of the function to a string, and as toString methods of
functions objects traditionally return the function's source code (more
or less) the string executed by the timeout would be a string of
javascript code that defined a function. No error would be expected
when this was executed, although no actual effects would be expected
either.

Richard.
 
S

samuelberthelot

Nevermind, if you live in Europe you are probably desintegrating because of
the heat just like me. ;-)

It's bloody hot indeed .. :)

As Richard said, it works for me... No error at all, whether with IE or
FF. That bit of code works just fine. Actually as I keep saying there
is nothing wrong with my code. However, it should be modified to
accommodate new requirement, which is allowing the gif to animate even
when the cpu intensive task is being executed. And THAT is my problem
:)
Anyone knows how I could do that ?

cheers,
Sam
 
E

Erwin Moller

Richard said:
Erwin Moller wrote:


This is not true, at least from IE 4, Netscape 4, opera 6 and many
others. A function reference may be the first argument to setTimeout,
Indeed it can be argued that a function reference is a better argument
to use, partly because it can be executed faster (not needing
interpreting into code) and partly because a string object can be
provide with a custom toString method that would return a string that
effectively called the function, providing optimum performance plus
full backward compatibility with older setTimeout implementations that
only accept string arguments.

Hi Richard,

Maybe that is true, maybe not.
My wisdom comes from O'Reilly's "Javascript, the definitive Guide, 4th
edition, covers Javascript 1.5".
It states clearly it want a string (page 679), no notes about a
functionreference whatsoever.

So I won't ague that you are wrong, I just want to note that is not
described in 'the guide'.

<snip>

Even on an implementation that only accepts string arguments the effect
of passing a function reference would be expected to be implicit
type-conversion of the function to a string, and as toString methods of
functions objects traditionally return the function's source code (more
or less) the string executed by the timeout would be a string of
javascript code that defined a function. No error would be expected
when this was executed, although no actual effects would be expected
either.

Aha, that clears that mystery up then!

Is this the same reason you can pass a function-reference to setTimeout?
Becuase it is converted via toString() to (a copy of?) the original
function?

Thanks for the explanation.

Regards,
Erwin Moller
 
R

Richard Cornford

Maybe that is true, maybe not.
My wisdom comes from O'Reilly's "Javascript, the definitive Guide, 4th
edition, covers Javascript 1.5".
It states clearly it want a string (page 679), no notes about a
functionreference whatsoever.

So I won't ague that you are wrong, I just want to note that
is not described in 'the guide'.

The "definitive guide" is only the least bad javascript book available,
that doesn't meant it is good, comprehensive, accurate, wise or
definitive.

Aha, that clears that mystery up then!

Is this the same reason you can pass a function-reference to setTimeout?
Becuase it is converted via toString() to (a copy of?) the original
function?
<snip>

No, the source code for a function declaration or a function expression
will be just a function declaration or a function expression; you need
call operators in addition to actually execute them.

Richard.
 
L

Lasse Reichstein Nielsen

[function parameter to setTimeout/setInterval]
My wisdom comes from O'Reilly's "Javascript, the definitive Guide, 4th
edition, covers Javascript 1.5".
It states clearly it want a string (page 679), no notes about a
functionreference whatsoever.

It's just a reference book, not the definition.
For authoritative definitions by the people actually implementing setTimeout,
see:
<URL:http://developer.mozilla.org/en/docs/DOM:window.setTimeout>
<URL:http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/settimeout.asp>

/L
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top