PDA

View Full Version : setTimeout() help



tbobker
11-18-2008, 10:52 AM
I am trying to create a onmouseover that is active after a certain amount of time. It does not seem to work.

My code below:



<html>
<head>

<script type="text/javascript">

function hiddentext(item)
{
var t=setTimeout(open(item),10000)

}
function open(banner){
document.getElementById(banner).style.display='block';
}

function clear(){
cearTimeout(t);
}
</script>


</head>
<body>

<div id="panel" style="background:#cccccc;display:none;">
Some information
</div>

<a href="#" onmouseover="hiddentext('panel');" onmouseout="clear();">Open</a>


</body>
</html>


Any help appreciated

rangana
11-18-2008, 11:53 AM
var t=setTimeout('open('+item+')',10000)

tbobker
11-18-2008, 12:59 PM
HI Thanks for reply.

I thought you only need + for concatenate.

Im not doing that just sticking the variable straight in. It doesnt work anyway.

Its just the delay that is not working?

rangana
11-18-2008, 02:09 PM
10 seconds is very long, anyway:


function hiddentext(item)
{
open(item);

}
function open(banner){
var t=setTimeout(function()
{
document.getElementById(banner).style.display='block';
},1000); // Change second param if you intend to work on 10sec.
}
function clear(){
cearTimeout(t);
}

tbobker
11-18-2008, 03:19 PM
HI mate,

THanks works a treat.

But i dont understand why you use this "function()"?

Are you creating a new function inside the event?

Thanks,

jscheuer1
11-18-2008, 05:29 PM
The setTimeout() method executes whatever it is set to execute in the global scope. Unless it has a parameter(s) fed to it at the time it is created, it will only have the global scope to look to for their values. Using string concatenation (will not work with objects) is one method for limiting the scope to a value that exists in the scope where setTimeout is created. Creating a new function (preferred - works with all types of values) in a scope where the parameter is defined and cannot change is another.

Twey
11-19-2008, 09:37 AM
On your original mistake, http://fn-js.info/snippets/bind has some info, as well as a helper function to make the whole thing a little simpler.