Jump to content

Class setInverval


The Little Guy

Recommended Posts

How do I add an interval to a class?

 

function Class(o){
this.cx;
this.objct = o;
this.obj;
this.slide 		= slideMethod;
this.sld 		= sl;
}
function slideMethod(e, time){
this.obj = document.getElementById(this.objct);
this.obj.style.position = 'absolute';
this.cx = this.obj.style.left+1-1;
setInterval('this.sld()', mil);       // Line Giving me the error message
}

function sl(){
alert(this.cx);
}

 

The error message I am getting is:

 

Error: this.sld is not a function

 

Anyone know why?

Link to comment
Share on other sites

OK, so I tried this:

 

function Class(o){
   this.cx;
   this.objct = o;
   this.obj;
   this.slide       = slideMethod;
   this.sld       = sl;
}
function slideMethod(e, time){
   var mil = 2000;
   this.obj = document.getElementById(this.objct);
   this.obj.style.position = 'absolute';
   this.cx = this.obj.style.left+1-1;
   setInterval('this.sld', mil);       // Line Giving me the error message
}

function sl(){
   alert(this.cx);
}

 

Nothing happens, You should be able to see that the result I am looking for is an alert every 2 seconds.

Link to comment
Share on other sites

Function calls made with setInterval are performed in the global scope, and as such, the use of this is impossible, because it only exists (and has the meaning you expect it to have) within an object.

 

function Class(o){
   this.cx;
   this.objct = o;
   this.obj;
   this.slide       = slideMethod;
   this.sld       = sl;
}
function slideMethod(e, time){
   this.obj = document.getElementById(this.objct);
   this.obj.style.position = 'absolute';
   this.cx = this.obj.style.left+1-1;
   setInterval('this.sld()', mil);       // Line Giving me the error message
}

function sl(){
   alert(this.cx);
}
this.sld(); // this is basically what you are trying to do. here, it should be obvious that it is impossible, though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.