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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    multiple timeouts?

    I don't know how to work with timeouts. I have a simple blinking image. but I'm not sure how to set multiple timeouts?

    Basically I want the image to blink to another one for 400, then return to the first image for 5000

    here's the basic starting point I have:

    Code:
    <script type="text/javascript">
    
    	var img_off = new Image();  img_off.src = 'images/blink.png';
    	var img_on = new Image();   img_on.src  = 'images/orho2.png';
    
    	function blink() {
    		var e = document.getElementById("blinking_orho");
    		e.src = (e.src == img_on.src) ? img_off.src : img_on.src;
    		setTimeout("blink();", 5000,);
    	}
    
    </script>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,478
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Replace

    Code:
    e.src = (e.src == img_on.src) ? img_off.src : img_on.src;
    setTimeout("blink();", 5000,);
    with

    Code:
    if (e.src == img_on.src) {
       e.src = img_off.src;
       setTimeout(blink, 400);
    } else {
       e.src = img_on.src;
       setTimeout(blink,5000);
    }
    or switch the numbers the other way around if that's what you wanted (it isn't clear which image is to display for which interval).

    Note that setTimeout expects a function as the first parameter, not a string.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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