Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setTimeout function

Status
Not open for further replies.

GabberGod

IS-IT--Management
Nov 16, 2003
73
AU
m having a problem with a section of javascript.

The code is completely function, but the menus disappear alittle too fast
when you mouseoff. So what I want to do is slow down the function.


lhide('proj_man', 'menu2' );

function lhide(s_menu, k_menu) {
setTimeout("document.getElementById(s_menu).style.visibility
= 'hidden'", 1000)
setTimeout("document.getElementById(k_menu).style.visibility
= 'hidden'", 500)
}

This is the code im using to do it. basicly the function works without the
setTimeout function call, so there must be something wrong with the way i've
written the call, can anyone help me.

This piece of code returns an error on line1 char 1 (which is incorrect as
it isnt positioned at line 1 char 1. It also says 's_menu' is undefined. but
s_menu is a variable why should this call try and access it as a string???

any help would be greatly appreciated.

Regards
Joseph Atie
 
Yup, the timeOut creates a seperate instance outside the function where the values s_menu and k_menu don't exist.

I guess you could use global vars, or insert the timeOut where the event fires*, or reference back to the same function.

* probably what I would do :p

----------
I'm willing to trade custom scripts for... [see profile]
 
update:

lhide('proj_man', 'menu2' );

function lhide(s_menu, k_menu) {
setTimeout("document.getElementById("s_menu").style.visibility
= 'hidden'", 1000)
setTimeout("document.getElementById("k_menu").style.visibility
= 'hidden'", 500)
}

Note the quotation marks arround the variables, i read somewhere else that you need to stop the function from parsing the whole arg as a string. This fixed the variable prob i was having.

Now its saying: expected ')' @ char 43.

Anyone see whats wrong with this???

Can anyone think of a better way???

 
setTimeout("document.getElementById("+s_menu+").style.visibility
= 'hidden'", 1000)


----------
I'm willing to trade custom scripts for... [see profile]
 
already a step ahead of you.

it still doesnt work.

takes me back to the original error of line 1, char 1, object expected :S
 
I didn't think it would work, that's why I didn't suggest it in my original reply.

Just use onmouseout="setTimeout('ihide()',1000);" or whatever. It's the easiest solution :)

----------
I'm willing to trade custom scripts for... [see profile]
 
damn :( theres like 100 options in the menu, thats alot of code

o well, if theres no way i guess then i will :p
 
nope, stil can't make this work, I tried these two combos:

onMouseOut="setTimeout("lhide('"+ proj_man +"', '"+ menu2 +"')", 1000);"

onMouseOut="setTimeout("lhide('proj_man', 'menu2')", 1000);"

They both return object expected for the next line of code.

Any sugestions???
 
Try:

Build the arg string then insert in the setTimeout,

cmd = "lhide('proj_man ', 'menu2')";

setTimeout(cmd,1000);

There's a good FAQ about arg's.

Maybe that'll do.


2b||!2b
 
Adding missing quotes to stormbind's second reply should work:

setTimeout("document.getElementById('"+s_menu+"').style.visibility
= 'hidden';", 1000);
 
Kinda strange, don't know why it's in the Date Section.


Here's the short of it:
Code:
<html><head><script>
function shw(val,str){
alert('This is '+val+' and this is '+str);
}
var cmd = &quot;shw('first arg','sec arg')&quot;;
setTimeout(cmd,2000);
</script></head><body>
TEST</body></html>

2b||!2b
 
>> Adding missing quotes to stormbind's second reply should work.

Oops, well it was something X:00AM so can I be forgiven? :/

Well you could also recur the function. Don't see why that wouldn't work and doesn't require a lot of editing.

function pass(key) {
if (!key) {setTimeout('pass(key)',1000);return true;}
}

onclick=&quot;pass()&quot;

----------
I'm willing to trade custom scripts for... [see profile]
 
>> Don't see why that wouldn't work
same reason: &quot;key&quot; no longer exists

would need to change to
if (!key) {setTimeout('pass(&quot;'+key+'&quot;)',1000);return true;}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
jemminger, the key would exist because it's hardcoded into the setTimeout. i'm sorry i made it look like i was using a variable name - that wasn't the intention.

yours is equally errored because the key (variable) no longer exists when it fires the timed command.

the answer is as follows, i think :)

if (!key) {setTimeout('pass(&quot;keyValue&quot;)',1000);return true;}

----------
I'm willing to trade custom scripts for... [see profile]
 
stormbind,
ok, i think i understand - you were just giving a very abstract example?

>> yours is equally errored because the key (variable) no longer exists when it fires the timed command.
on the contrary, i was treating &quot;key&quot; as a string var, and re-passing it as a string (note the single _and_ double quotes) ;-)

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top