SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)

    Passing Varibale Value from a Function through to an Interval (setInterval)

    So I have a function to toggle some "drawers" on my webpage.

    The "toggle" function is meant to check if a "drawer" is open or not. If not it will call another function to do the opening via an interval.

    The problem is that for some odd reason, when I'm trying to pass the ID of the drawer to open through to the "open" function, the variable name itself is being interpreted as string data (I suppose !!).

    Here's an example...


    Code:
    function toggleRecordDrawer(drawerID)
    	{
    	openInterval = window.setInterval('openRecordDrawer(drawerID)',1000);
    	}
    
    
    function openRecordDrawer(drawerID)
    	{
    	tempDrawerID = 'TinyTable_Data_' + drawerID;
    
    	yDim = document.getElementById(tempDrawerID).style.height;
    	yDim = parseInt(yDim);
    	yDim = (yDim * 1);
    
    	newYDim = (yDim + 10);
    
    	finalYDim = (200);
    
    	if(yDim <= finalYDim)
    		{
    		document.getElementById(tempDrawerID).style.height = newYDim + "px";
    		}
    	else
    		{
    		window.clearInterval(openInterval);
    		}
    	}
    Everytime the open function fires I get the error, "DrawerID is not defined".

    Does anyone have any idea how I can get this to work ?

    I have a feeling that I need to fix my syntax but I've been playing with it for the past half hour with no favourable result.

    Any ideas ?

    Thanks.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  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)
    setTimeout does things in a separate scope so you have to create a closure:
    Code:
    function toggleRecordDrawer(drawerID) {
      openInterval = window.setInterval(function() {
        openRecordDrawer(drawerID);
      },1000);
    }
    Also, global variables are evil!

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Try passing the actual value of drawerID instead.

    Code Javascript:
    openInterval = window.setInterval('openRecordDrawer(' + drawerID' + )',1000);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles
    setTimeout does things in a separate scope so you have to create a closure:
    Code:
    function toggleRecordDrawer(drawerID) {
      openInterval = window.setInterval(function() {
        openRecordDrawer(drawerID);
      },1000);
    }
    Also, global variables are evil!
    ooore...

    That makes sense.

    Thanks a lot, man. It works now.




    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

  5. #5
    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)
    Quote Originally Posted by pmw57 View Post
    Try passing the actual value of drawerID instead.

    Code Javascript:
    openInterval = window.setInterval('openRecordDrawer(' + drawerID' + )',1000);
    This way of doing it seem a bit dirty, almost eval-ish.

    Besides, if you indent to adhere to Douglas Crockford's code conventions (like you recommended over here), you shouldn't pass strings to setInterval (see the last line in Crockford's document).

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Thanks Raffles, I was trying wanting to use an anonymous function but didn't want to complicate things. As you nicely point out though, other complications arise.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •