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:
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:obj.active = window.setInterval("colorChange()", 1000, obj);
I don't know what i did wrong, please give me a hand and look into this.Code:Fehler: obj has no properties
Greets
Jens
Here the whole example:
And my debuggin output: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>
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) ...


Reply With Quote
Bookmarks