what i want to do is this:

function foo(param1, param 2){
[INDENT]this.bar=function(){
[INDENT]this.intervalID=setInterval(this.fbar,5000);[/INDENT]
}

this.fbar=function(){
[INDENT]//(do stuff with the properties of "this"
alert(this.intervalID);[/INDENT]
}
[/INDENT]

}

but setInterval will not carry over the properties of "this".
when this happens:

var apple=new foo(5,27);
apple.bar();

i need apple.fbar(); to repeat automatically by force of apple.bar();

note: fbar does not have to be inside of the foo function but i do need to get all of "this"'s properties to it durring a setInterval
thanks for the help!

Recommended Answers

All 5 Replies

You have a scoping problem.

Your values created in foo are local to that function. They don't exist outside the bounds of that function.

Declare some global variables at the top of your .js file. Then assign the values created inside foo to those variables.

thanks, i did alittle bit of googling on js globals and found i could attach the variable on to the window object. so.. my script ends up looking like this.... maybe?

function foo(param1,param2){
 window.currentObject=this;
 window.currentObject.fruit=param1;
 window.currentObject.newfruit=param2;

    window.currentObject.bar=function(){

       window.currentObject.intervalID=setInterval("window.currentObject.fbar();",5000);

    } 
    window.currentObject.fbar=function(){
   
          var blah=window.currentObject.fruit;
          window.currentObject.fruit=window.currentObject.newfruit;
          window.currentObject.newfruit=blah;
        

    } 
     window.currentObject.end=function(){
           clearInterval(window.currentObject.intervalID);
    }


}
var apple=new foo(apple,orange);
apple.bar();
setTimeout("apple.end();",60000);

i guess here is my new question. how can i transfer all of the properties of window.currentObj to 'this'?

you can do like this:

function foo(param1, param 2){
    var me = this;
    this.bar=function(){
        this.intervalID=setInterval(function(){me.fbar.call(me);},5000);
    }

this.fbar=function(){//(do stuff with the properties of "this"
alert(this.intervalID);}

}

To get around these scope issues, you can either go with the messier and error prone global object solution or make use of the elegant concept of Closures.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv"Script-Content-Type" content="text/javascript">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="Expires" content="0"> <!-- disable caching -->
    <title>Example</title>
    <script type="text/javascript">
    function Klass(name) {
      this.name = name;
      this.handle = null;
      this.startTimer = function() {
        this.handle = setInterval(function(obj) {
          return(function() {
            alert(obj.name);
            });
          }(this), 5000);
      }
    var a = new Klass('hello');
    a.startTimer();
    </script>
</head>
<body>  
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.