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
https://forums.phpfreaks.com/topic/153815-class-setinverval/
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
https://forums.phpfreaks.com/topic/153815-class-setinverval/#findComment-808955
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
https://forums.phpfreaks.com/topic/153815-class-setinverval/#findComment-809095
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.