The Little Guy Posted April 13, 2009 Share Posted April 13, 2009 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? Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted April 13, 2009 Share Posted April 13, 2009 that is because it is not a function its a variable called from within a function Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted April 13, 2009 Author Share Posted April 13, 2009 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. Quote Link to comment Share on other sites More sharing options...
Kieran Menor Posted April 14, 2009 Share Posted April 14, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.