User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 373,936 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,542 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 752 | Replies: 5
Reply
Join Date: Mar 2007
Location: georgia
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

setInterval inside function issue

  #1  
Apr 16th, 2008
what i want to do is this:

function foo(param1, param 2){
this.bar=function(){
this.intervalID=setInterval(this.fbar,5000);
} this.fbar=function(){
//(do stuff with the properties of "this" alert(this.intervalID);
}
}
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!
Last edited by hunkychop : Apr 16th, 2008 at 5:29 pm.
toast
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2007
Posts: 2,433
Reputation: MidiMagic is on a distinguished road 
Rep Power: 6
Solved Threads: 99
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Posting Maven

Re: setInterval inside function issue

  #2  
Apr 16th, 2008
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.
Last edited by MidiMagic : Apr 16th, 2008 at 8:03 pm.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Mar 2007
Location: georgia
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Re: setInterval inside function issue

  #3  
Apr 16th, 2008
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);
Last edited by hunkychop : Apr 16th, 2008 at 8:21 pm.
toast
Reply With Quote  
Join Date: Mar 2007
Location: georgia
Posts: 55
Reputation: hunkychop is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
hunkychop's Avatar
hunkychop hunkychop is offline Offline
Junior Poster in Training

Re: setInterval inside function issue

  #4  
Apr 16th, 2008
i guess here is my new question. how can i transfer all of the properties of window.currentObj to 'this'?
toast
Reply With Quote  
Join Date: Apr 2008
Posts: 1
Reputation: wuzhiye is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wuzhiye wuzhiye is offline Offline
Newbie Poster

Re: setInterval inside function issue

  #5  
Apr 17th, 2008
you can do like this:

  1. function foo(param1, param 2){
  2. var me = this;
  3. this.bar=function(){
  4. this.intervalID=setInterval(function(){me.fbar.call(me);},5000);
  5. }
  6.  
  7. this.fbar=function(){//(do stuff with the properties of "this"
  8. alert(this.intervalID);}
  9.  
  10. }
Last edited by peter_budo : Apr 19th, 2008 at 8:35 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: setInterval inside function issue

  #6  
Apr 17th, 2008
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>
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 6:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC