2 Replies Latest reply on Jun 23, 2007 6:54 PM by kglad

    Class Coding Problems

    Cobalt1423
      ok, JESUS this is pissing me off sooo much!

      so i made this thing (i wont post it all here) and it works perfectly.
      it uses an init() function, which sets the Interval for the test() function. now the test() function requires an outside variable, which is defined earlier, and it can easily get it. then when its done, it uses the clearInterval() function and it stops it. No Problems!


      quote:

      var hold;

      function init()
      {
      hold = setInterval(process, 200);
      }

      function process()
      {
      trace("ok");
      clearInterval(hold);
      }

      init();



      Now i try to make it into a class!

      quote:

      class demo
      {

      var hold;
      var testvaribale = "asd123";

      function init()
      {
      this.hold = setInterval(process, 200, this.testvaribale);
      }

      function process(testvar)
      {
      trace(testvar);
      clearInterval(this.hold);
      }
      }

      import demo;
      var Demo = new demo();
      Demo.init();



      there are two MAJOR problems:
      1) the process() function run by itself CAN grab any variable, example: trace(this.testvariable); would output "asd123". but the process() function run by the init() using the interval, can NOT grab any variables. the previous code would output "undefined". i can pass variables through the setInterval code, and that has worked up until problem #2
      2) clearInterval() does NOT work at all. using hold, this.hold, or hold2 (a variable passed throught the setInterval code), it will not clear the interval.


      can someone please help me with this?
        • 2. Re: Class Coding Problems
          kglad Adobe Community Professional & MVP
          you're loosing scope when you use setInterval(). it works the same even outside of class files: whenever you call a function using that form of setInterval(), the scope and the word "this", within the function are undefined.

          to remedy, you can use the 2nd form of setInterval() which passes the scope to your function as the first parameter, the function name (AS A STRING) in the 2nd parameter, the interval frequency in ms as the 3rd parameter followed by any number of variables.

          for example: