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 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    Bristol, UK
    Posts
    85
    Thanks
    6
    Thanked 6 Times in 6 Posts

    setTimeout - newbie question

    Hi, I've had a search and couldn't find the answer, so apologies if I'm repeating an already answered question...

    I'm trying to get something really simple working, code is:

    <html>
    <head>
    <script language="JavaScript">
    function switchswap(swno) {
    document.switchform.switchtext.value="You pushed switch no. " + swno;
    setTimeout(document.switchform.switchtext2.value="Switch no. " + swno + " was swapped back",1000);
    }

    </script>

    </head>
    <body>
    <a href="javascript:switchswap(1)">No. 1</a>
    <a href="javascript:switchswap(2)">No. 2</a>

    <form name="switchform">
    <input type="text" id="switchtext" value="0" size="40">
    <input type="text" id="switchtext2" value="0" size="40">
    </form>
    </body>
    </html>

    All I want to happen is for an event to be fired, then another to fire at a preset time. I've put the text boxes in to test for now. All that happens is that both events happen at the same time.

    I've tried separating the functions and calling the 2nd one from within the first and the same thing happens. It doesn't seem to make any difference if I call a function from within the setTimeout or whether I put the code in.

    Any help would be greatly appreciated.

    Thanks,

    Glenn

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    You can use an anonymous function in the setTimeout
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Using an anonymous function in setTimeout</title>
    <script type="text/javascript">
    function switchswap(swno)
    {
    	document.switchform.switchtext.value="You pushed switch no. " + swno;
    	setTimeout
    	(
    		function()
    		{
    			document.switchform.switchtext2.value="Switch no. " + swno + " was swapped back"
    		}
    		,1000
    	);
    	return false;
    }
    </script>
    
    </head>
    <body>
    <a href="#" onclick="return switchswap(1);">No. 1</a>
    <a href="#" onclick="return switchswap(2);">No. 2</a>
    <form name="switchform">
    	<input type="text" id="switchtext" value="0" size="40">
    	<input type="text" id="switchtext2" value="0" size="40">
    </form>
    </body>
    </html>
    http://phrogz.net/JS/Classes/Creatin...Functions.html
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Nov 2006
    Location
    Bristol, UK
    Posts
    85
    Thanks
    6
    Thanked 6 Times in 6 Posts
    Thank you thank you thank you.

    Awesome, exactly what I wanted.

    And thanks for the timely reply.

    Glenn


  •  

    Posting Permissions

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