Web Analytics Made Easy -
StatCounter Object Problems when using setTimeout - CodingForum


No announcement yet.

Object Problems when using setTimeout

  • Filter
  • Time
  • Show
Clear All
new posts

  • Object Problems when using setTimeout


    The objects I've created r fairly big so I hope the following
    represents the problem ok.

    I've got a custom object as follows:

    //filename is myscript.js

    function ChangeCount(count)
    this.count += 1;
    setTimeout("ChangeCount(this.count);", 100);

    function Obj()
    this.count = 1;
    this.ChangeCount = ChangeCount;

    //global var

    function SetUp()
    ObjHolder = new Obj;

    <script src="myscript.js"></script>

    <body onLoad="SetUp();">

    <img onClick="ObjHolder.ChangeCount(2);" />


    The problem is that when the setTimeout fires it calls the
    ChangeCount function as expected but the variable this.count
    is now invalid because it is not of the same object instance
    as the ObjHolder variable.

    Help please!!!!!!
    Last edited by SirFrederickII; Mar 6, 2004, 10:18 AM.

  • #2
    Don't have time to look at the code right now,but what I can say is that I think this is actually a bug of the scripting engine in IE(not sure about other browsers tough),in which u can't call a function wih parameters inside a setTimeout()

    function F(msg){alert(msg);}
    //This won't work

    A possibility would be (I *think*),to use the function object....
    Not sure if that would work but worth to give it a try...