473,473 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

setTimeout doesn't seem to fire.

Is there any obvious reason why there's no delay in the execution of
setting the id.style.display when this function gets called?

function Hide(divId,timeout)
{
var id = document.getElementById(divId);
setTimeout(id.style.display = 'none',timeout);
}

Thanks for any help.

--Brent
Dec 19 '05 #1
6 2219

Brent wrote:
Is there any obvious reason why there's no delay in the execution of
setting the id.style.display when this function gets called?

function Hide(divId,timeout)
{
var id = document.getElementById(divId);
setTimeout(id.style.display = 'none',timeout);
}

Thanks for any help.

--Brent


setTimeout expects a string in the first parameter. What you have
given is not a string. Change it like so:

setTimeout("id.style.display = 'none'", timeout);

One thing you should note is that, this will still not work.
setTimeout executes in the global scope. Once your function Hide has
reached the end, your variable 'id' will no longer be available in this
context.

Dec 19 '05 #2

Brent wrote:
Is there any obvious reason why there's no delay in the execution of
setting the id.style.display when this function gets called?

function Hide(divId,timeout)
{
var id = document.getElementById(divId);
setTimeout(id.style.display = 'none',timeout);
}


Yeah: the first argument to setTimeout should be a string; so the
expression "id.style.display = 'none'" is evaluated; setTimeout
sees the arguments "none" and [the value of] timeout. You probably
want to say

setTimeout(function() {id.style.display='none'},timeout);

hj

Dec 19 '05 #3
VK

Brent wrote:
Is there any obvious reason why there's no delay in the execution of
setting the id.style.display when this function gets called?

function Hide(divId,timeout)
{
var id = document.getElementById(divId);
setTimeout(id.style.display = 'none',timeout);
}


Yammy! I'll be first before the crowd! :-))

setTimeout() first argument can be
1) string literal like setTimeout("myFunction(param)",1000)
2) function reference like setTimeout(myFunction,1000)
/* that a newer one so some old browser may not support this */
3) an expression evaluated to (1) or (2) from above

You have an expression so in order to prepare setTimeout call
interpreter has to execute first id.style.display = 'none';

setTimeout("id.style.display = 'none';",timeout)
will fix your problem issue

Dec 19 '05 #4
VK
> Yammy! I'll be first before the crowd! :-))

Need to improve my typing speed :-(

:-)

Dec 19 '05 #5
VK wrote:

Brent wrote:
Is there any obvious reason why there's no delay in the execution of
setting the id.style.display when this function gets called?

function Hide(divId,timeout)
{
var id = document.getElementById(divId);
setTimeout(id.style.display = 'none',timeout);
}


Yammy! I'll be first before the crowd! :-))

setTimeout() first argument can be
1) string literal like setTimeout("myFunction(param)",1000)
2) function reference like setTimeout(myFunction,1000)
/* that a newer one so some old browser may not support this */
3) an expression evaluated to (1) or (2) from above

You have an expression so in order to prepare setTimeout call
interpreter has to execute first id.style.display = 'none';

setTimeout("id.style.display = 'none';",timeout)
will fix your problem issue


Sorry, no; as web.dev pointed out, this will run in the global
scope, where it's likely that id will be undefined.

If you really want to do this by passing a string rather than a
function reference to setTimeout, try:

function Hide(divId,timeout) {
hider = new Function("var elm=null;" +
"if (document.getElementById) " +
"elm=document.getElementById('"+id+"');" +
"if (elm && elm.style) elm.style.display='none';");
setTimeout("hider()",timeout);
}

Multiple invocations will redefine the "hider" function, however,
so it's far safer to pass a function reference. If that's simply
not possible, do your best to ensure a unique name for each time
you run "Hide".

hj

Dec 19 '05 #6
Howard Jess <howard..@dhitechnologies.dot.com> writes:
If you really want to do this by passing a string rather than a
function reference to setTimeout, try:

function Hide(divId,timeout) {
hider = new Function("var elm=null;" +
"if (document.getElementById) " +
"elm=document.getElementById('"+id+"');" +
"if (elm && elm.style) elm.style.display='none';");
setTimeout("hider()",timeout);
}
No need to use the Function constructor here. A local function
declaration will do just as well, and should work everywhere
the outer declaration does.

function hide(divId, timeout) {
function localHider() {
var elem = nukk;
if (document.getElementById) {
elm = document.getElementById(divId);
} // else fallback to document.all or even document.layers
if (elm) {
var estyle = elm.style || elm;
estyle.display="none";
}
}
hider = localHider;
setTimeout("hider();", timeout);
}
Multiple invocations will redefine the "hider" function, however,
so it's far safer to pass a function reference.
Absolutely.
If that's simply not possible, do your best to ensure a unique name
for each time you run "Hide".


You could build your own abstraction over setTimeout that works either
way:
---
timer = new Object();
timer.nextCtr = 0;
timer.funcs = new Array();
timer.setTimeout = function _setTimeout(func, time) {
var id = this.nextCtr++;
if (typeof func != "function") { // not a function, convert to string
timer.funcs[id] = new Array(setTimeout("delete timer.funcs["+id+"];"+func, time));
return id;
}
// a function:
function wrapper() {
delete timer.funcs[id];
func();
}
wrapper.toString = function () {
return "timer.funcs["+id+"][1]()";
};
timer.funcs[id] = new Array(setTimeout(wrapper, time), wrapper);
return id;
};
timer.clearTimeout = function _clearTimeout(id) {
var pair = timer.funcs[id];
if (pair) {
delete timer.funcs[id];
clearTimeout(pair[0]);
}
}
---

You can then use it as, e.g.:

timer.setTimeout(function(){alert("X");},2000);
var id = timer.setTimeout(function(){alert("Y");},1500);
timer.setTimeout(function(){alert("Z");},1000);
timer.clearTimeout(id);
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: VA | last post by:
Suppose I have setTimeout(foo(),n); long_running_function(); Would foo() be executed even when long_running_function() is still executing? In other words, does the setTimeout() pre-empt...
6
by: cjl | last post by:
Hey all: In my script I have the following: change_opacity (0, id); for (var x = 0; x<=100; x=x+1) { change_opacity( x, id ); }
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
3
by: Darren.Ratcliffe | last post by:
Hi The below pasted code is my attempt to get the text of a span to change every second. However, it seems to just set the text of the span to be the last item in the array. Can anybody...
4
by: Lester | last post by:
I need to fire a function 4 times a second using setTimeout()), but I have an HTML-editor on the page (FCKeditor) which has hints over its buttons. However, the rapid timer never allows these hints...
18
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and...
6
by: Max | last post by:
Hi, I'm not exactly sure why this doesn't work. I'm basically just trying a simple approach at a slide down div. function slide_div { ...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
18
by: Csaba Gabor | last post by:
Is there a straightforward way of implementing a PHP equivalent to window.setTimeout? In a javascript web app, it's often the case that things are done on an event driven basis. For example,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.