www.webdeveloper.com
Results 1 to 6 of 6

Thread: passing variables to multiple functions

  1. #1
    Join Date
    Sep 2010
    Posts
    10

    passing variables to multiple functions

    Hello

    I'm new here, and new to js.

    Here is my problem: I have written out a code to make an image switch from state 0 to 1 and back to 0 again (an eye blink).

    The code works fine, but I would like to write the functions with arguments so it could be applied to more images. I have tried for a few hours (and searched forums) and am getting no where. Here's my code. Thanks!


    Code:
    function home_blinkDown()
    {
    	//alert('blink down');
    	var t = setTimeout("home_blinkSwap('home_js', 'images/main/home_blink.png')", 2000);
    }
    
    function home_blinkSwap(img_name, img_src)
    {
    	document[img_name].src = img_src;
    	var t = setTimeout("home_blinkUp('home_js', 'images/main/home.png')", 400);
    }
    
    function home_blinkUp(img_name, img_src)
    {
    	document[img_name].src = img_src;
    	var t = setTimeout("home_blinkDown()", 2000);
    }
    
    // ...
    
    <body onload="home_blinkDown" >

  2. #2
    Join Date
    Jul 2010
    Posts
    135
    Hi,
    depends on how you want to work with the images. What is the main goal? You want a photoalbum? How many images? You can call main function like
    Code:
    onload="main function("1.jpg","2.jpg","3.jpg","4.jpg",...)"
    and then use
    Code:
    for (pic in arguments) myfunc(pic)
    Then we need to know what type of event you want to use? Do you want do use onclick or just do "auto preview" action? Do you want to click on the images or just to see them showing and hiding?

  3. #3
    Join Date
    Sep 2010
    Posts
    10

    thanks for the reply

    Thanks for the reply crazy_boy.

    I just want the images (which are eyes) to blink every couple of seconds. So the main function is just an 'onload' and then the three functions loop via the timers calling them.

    Here is an example of how I tried to make a similar function reusable:

    Code:
    function test(alertVar, time)
    {
    	var t=setTimeout("testTwo('alertVar', 'time')", time);
    }	
    	
    function testTwo(alertVar, time)
    {
    	var t=setTimeout("test('alertVar', 'time')", time);
    	alert('alertVar');
    }
    
    // ...
    
    <body onload="test('blah', '200')" >

    The problem here is that the 'alertVar' is shown as "alertVar" (not "blah") when the alert is called in 'testTwo'. So, the variable isn't being passed...

    Thanks again everyone.

  4. #4
    Join Date
    Apr 2009
    Posts
    96
    var t=setTimeout("test('"+alertVar+"', '"+time+"')", time);

  5. #5
    Join Date
    Sep 2010
    Posts
    10
    Thanks Rufe0. I'll try that.

  6. #6
    Join Date
    Sep 2010
    Posts
    10
    DUUUUUUDE(ETTE) Thank you so much!!!!! I've been killing myself on this. I've never seen the
    Code:
    '"+...+"'
    syntax before...

    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles