www.webdeveloper.com
Results 1 to 3 of 3

Thread: Problem with passing Arg with setInterval()

  1. #1
    Join Date
    Aug 2007
    Location
    Germany, NRW
    Posts
    76

    Question Problem with passing Arg with setInterval()

    Hi,

    i tried to pass an Object as an argument to a function that is periodical invocated by a setInterval() function. But the passing only worked on the inital invocation, in all that follow the arg is 'undefined'.
    The synopsis i used:
    Code:
    obj.active = window.setInterval("colorChange()", 1000, obj);
    The error console tells me that the error is in that line of the function, that uses the obj for the first time. So i asume the obj was not passed.
    Code:
    Fehler: obj has no properties
    I don't know what i did wrong, please give me a hand and look into this.

    Greets

    Jens

    Here the whole example:
    Code:
    <?xml version="1.0" encoding="iso-8859-1" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/chtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; iso-8859-1"/>
    
    <script type="text/javascript"> 
    /* <! [Cdata[ */
    	
    function colorChange(obj) {
     	 	 	 	
     	 if (obj.currentColor == 1) {
        	document.bgColor = obj.bgColor[1];
        	obj.currentColor = 2;
      	} else {
        	document.bgColor = obj.bgColor[0];
       		obj.currentColor = 1;
      	  }
      	  
     	obj.currentCount += 1;
     	
      	if (obj.currentCount >= obj.maxChanges)
      		window.clearInterval(obj.active);
      	
      	if(!obj.active) {
      		obj.active = window.setInterval("colorChange()", 1000, obj);
      	}
        
    }
    
    window.onload = function() {
    	
    	document.getElementById('startTheMess').onclick = function() {
    		var Color = {};
    		Color.maxChanges = 10;
    		Color.currentCount = 1;
    		Color.bgColor = ['yellow', 'aqua'];
    		Color.currentColor = 1;
    		Color.toString = function() {
    			return 'Object: Color maxChanges = '+Color.maxChanges+', currentCount = '+Color.currentCount+', bgColor = '+Color.bgColor;
    		}
    		
    		colorChange(Color);	
    	}
    }
    
    /* ]]> */
    </script>
    
    </head>
    <body>
    
    <h1>Color change with setInterval()</h1>
    <h3>Synopsis</h3>
    <p>window.setInterval(func, interval, args...)</p>
    <h3>Arguments</h3>
    <p>func: A javascript function to be periodically executed</p>
    <p>interval: in milli sec (integer) </p>
    <p>args: Any number of arbitrary values to be passed as arguments to each invocation of the func.</p>
    
    <h2 id="startTheMess">start</h2>
    
    </body>
    
    </html>
    And my debuggin output:

    Code:
    level 1: calling colorChange(Object: Color maxChanges = 10, currentCount = 1, bgColor = yellow,aqua)
    level 2: count: 2
    level 2: active set
    level 1: calling colorChange(undefined)
    level 1: calling colorChange(undefined)
    ...

  2. #2
    Join Date
    Aug 2007
    Location
    Germany, NRW
    Posts
    76
    I looked into it some more, so i can ask that question more specific.

    Is it possible to pass arbitrary args to a function invoked by setInterval():

    like this??
    Code:
    var active = window.setInterval('ANIMATION.animator()', aniObject.timing, aniObject);
    or is there an other way to do that?

    Greets

    Jens

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You may use an anonymous
    Code:
    obj.active = window.setInterval(function(){colorChange(obj)}, 1000);

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