...

View Full Version : Yet Another setTimeout not working



ubh
03-19-2009, 09:22 PM
Here we are again pondering why setTimeout will not work, or rather mucks things up.

This will work.

addOn(obj);

function addOn (obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0];
span.style.display="block";

thkOn(obj);

function thkOn(obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0].innerHTML = "Thank You!";
}


}


This will not

addOn(obj);

function addOn (obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0];
span.style.display="block";

setTimeout('thkOn(obj)',2000);

function thkOn(obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0].innerHTML = "Thank You!";
}


}

Any ideas?
Thanks.

Eldarrion
03-19-2009, 09:42 PM
The following should work:



<script type="text/javascript">
function addOn (obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0];
span.style.display="block";

setTimeout(function() {
thkOn(obj);
}, 2000);

function thkOn(obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0].innerHTML = "Thank You!";
}
}
</script>


It apparently doesn't like executing code directly. (Mind you, if you remove the single quotes you have in your timeout, it will execute, but without a delay, so...) Enjoy.

ubh
03-19-2009, 10:25 PM
Duhh assign an anonymous function to the time out HELLO!!

Yeah its all working just fine now, see what happens when you are trying to be all standard about your coding, nothing works ;) lol.

Also I did try not using single quotes before but still, never got the results I needed.

Thanks my friend!

Old Pedant
03-19-2009, 11:07 PM
Naturally, the anonymous function is the way to go for this, but I had to point out another way:


addOn(obj);

function addOn (obj)
{
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0];
span.style.display="block";

var id = obj.id;
if ( id == null )
{
id = "RO_" + Math.floor( 1000000 * Math.random() );
obj.id = id;
}

setTimeout('thkOn("' + id + '")',2000);

function thkOn(objid)
{
var obj = document.getElementById(objid);
var parentNode = obj.parentNode;
var span = parentNode.getElementsByTagName("span")[0].innerHTML = "Thank You!";
}


}

That should be hacky enough for anybody.

Old Pedant
03-19-2009, 11:09 PM
Trivia question:

What will be the value of the variable span upon exit from the function thkon( ) ?? (In either set of code.)

<grin style="evil evil" />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum