Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    Question Yet Another setTimeout not working

    Here we are again pondering why setTimeout will not work, or rather mucks things up.

    This will work.
    Code:
    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
    Code:
    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.

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    The following should work:

    Code:
    <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.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • Users who have thanked Eldarrion for this post:

    ubh (03-20-2009)

  • #3
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    Naturally, the anonymous function is the way to go for this, but I had to point out another way:
    Code:
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    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" />


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •