Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6770389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:18:23+00:00 2026-05-26T15:18:23+00:00

I have a class like: function run(){ this.interval; this.start = function(){ this.interval = setInterval(‘this.draw()’,1000);

  • 0

I have a class like:

function run(){
this.interval;
this.start = function(){
    this.interval = setInterval('this.draw()',1000);
};
this.draw = function(){
    //some code
};} var run = new run(); run.start();

however I can’t seem to reference/call this.draw() within the setInterval, it says this.draw() is not a function, and if I remove the quotes it says useless setInterval call, what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:18:23+00:00Added an answer on May 26, 2026 at 3:18 pm

    The value of this is set depending on how a function is called. When you call a function as a constructor using new then this will refer to the object being created. Similarly when you call a function with dot notation like run.start() then this will refer to run. But by the time the code run by the setInterval is called this doesn’t mean what you think. What you can do is save a reference to this and then use that reference, like the following:

    function Run(){
      var self = this;
    
      self.start = function(){
        self.interval = setInterval(function() { self.draw(); },1000);
      };
    
      self.draw = function(){
        //some code
      };
    }
    
    var run = new Run();
    
    run.start();
    

    Note also that you’ve created a function called run and a variable called run – you need to give them different names. In my code (bearing in mind that JS is case sensitive) I’ve changed the function name to start with a capital “R” – which is the convention for functions intended to be run as constructors.

    EDIT: OK, looking at the other answers I can see that just maybe I overcomplicated it and as long as draw() doesn’t need to access this it would’ve been fine to just say:

    this.interval = setInterval(this.draw, 1000);
    

    But my point about not giving your constructor and later variable the same name still stands, and I’ll leave all the self stuff in because it is a technique that you will need if draw() does need to access this. You would also need to do it that way if you were to add parameters to the draw() function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
I have an array of function callbacks, like this: class Blah { private var
Suppose I have a function which looks like this: template <class In, class In2>
Let's say I have some code like this in AS3 for(...) thing = new
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a class like this: public class Stretcher : Panel { public static
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
Let's say I have a class that looks something like this: class Foo(Prop1:Int, Prop2:Int,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.