Running a periodic function from an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • larztheloser
    New Member
    • Jan 2010
    • 86

    Running a periodic function from an object

    Hey everyone,
    Would anybody happen to know of a way to make this work:
    Code:
    function myObject()
    {
    this.someText="Hi!";
    this.period=setInterval("alertsometext();",2000);
    }
    function alertsometext()
    {
    alert(this.someText);
    }
    n=new myObject();
    Again I know there are other ways to do this (just calling a periodic "alert('hi' )", for instance), but this is for an animation class where I want to keep information about the layer packaged away in an object. Could I pass the object as an argument in the setInterval call (using the this keyword, somehow, I think)?

    Thanks in advance,
    Lars
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you’re better off with a closure here.
    Code:
    function myObject()
    {
        var someText = "Hi!";
        function alertsometext()
        {
            alert(someText);
        }
        this.period = setInterval(alertsometext, 2000);
    }
    
    var n = new myObject();

    Comment

    • larztheloser
      New Member
      • Jan 2010
      • 86

      #3
      Thanks! The error turned out to be that you can't do it when it is attached with the this operator, but with a general variable you can (I personally have a little gripe with nested functions, that they make my classes huge).

      Comment

      • larztheloser
        New Member
        • Jan 2010
        • 86

        #4
        WAIT - that variable is no longer tied to the object, now, is it? So you could run the event anywhere - in fact, in my testing someText now behaves like a global! I want someText to be inaccessible except through the object, because the wider application I'm doing this for will have multiple objects with multiple values of someText.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          it’s not a global, it’s a closure, there are worlds between. btw. have you tried to change the value?
          but it would not be difficult to write a function, that changes someText.

          Comment

          • larztheloser
            New Member
            • Jan 2010
            • 86

            #6
            No, what I mean is - well, basically I don't want this to work:
            Code:
            function myObject()
            {
            var someText = "Hi!";
            }
            obj=new myObject();
            alert(someText);
            So I can do have 2 objects with different values of sometext without them interfering with each other:
            Code:
            function myObject()
            {
            var someText = "Hi!";
            }
            obj1=new myObject();
            obj2=new myObject();
            obj2.someText="Hello!"; //throws an error because it doesn't exist
            Or is this sort of encapsulation beyond javascript?
            Last edited by larztheloser; Jan 21 '10, 10:37 PM. Reason: small code mistake

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              So I can do have 2 objects with different values of sometext without them interfering with each other:
              you already have that.

              but you still have to understand the concept of closures.

              so, let’s extent myObject, so that you can change the someText variable
              Code:
              // define someText while creating the object
              function myObject(txt)
              {
                  // this is a private variable
                  var someText = String(txt) || "Hi!";
                  // this is a private function
                  function alertsometext()
                  {
                      alert(someText);
                  }
                  // this is a public property
                  this.period = setInterval(alertsometext, 2000);
                  // a priviledged method to set someText
                  this.someNewText = function (newValue)
                  {
                      someText = newValue;
                  };
              }

              Comment

              • larztheloser
                New Member
                • Jan 2010
                • 86

                #8
                Wow. Javascript is even more powerful than I thought! Thanks, Dormilich, I shall give this a try!

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  recommended reading.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    you might even want to add some security to your setInterval ID, so that it can’t be deleted by accident.
                    Code:
                    function myObject(txt, delay)
                    {
                        var someText = String(txt) || "Hi!";
                        function alertsometext()
                        {
                            alert(someText);
                        }
                        this.someNewText = function (newValue)
                        {
                            someText = newValue;
                        };
                        // no need to make period public
                        var d = Number(delay) || 1000;
                        var period = setInterval(alertsometext, d);
                        this.stop = function ()
                        {
                            clearInterval(period);
                        };
                    }

                    Comment

                    Working...