SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing variable to setInterval

    Here is the code:

    Code:
    function slide(thisOne) {
    	var theObject = document.getElementById(thisOne);
    	var measureTop = parseInt(retrieveComputedStyle(theObject, "top"));
    		
    	if (measureTop == "0") {		
    		theObject.animationTimer = setInterval("moveObject(theObject, 0, 200, 25)", 50);
    	} else {
    		theObject.animationTimer = setInterval("moveObject(theObject, 0, 0, 25)", 50);
    	}
    }
    I am trying to use the variable "theObject" to pass a value to setInterval.
    But I keep getting the error that "theObject" is not defined. But I have tested it with an "alert" just before the "if" statement and I know that not only is it defined, but it has the correct value.

    Why won't setInterval recognise the variable? Is there a way to get this to work?

    Thanks.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    setInterval is a member of the window object and so if you pass parameters to it like that (as a string) they must be in the same scope (i.e. you can only pass global variables to it, since global variables are members of the window object). In order to do what you want, you have to pass a function:
    Code:
    theObject.animationTimer = setInterval(function() {
      moveObject(theObject, 0, 200, 25)
    }, 50);

  3. #3
    SitePoint Member
    Join Date
    May 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Works great

    Thanks Raffles!

    This works perfectly.

    Onward and upward!


Bookmarks

Posting Permissions

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