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 8492371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:41:58+00:00 2026-06-10T22:41:58+00:00

I have a function inside a object that sets up a interval that calls

  • 0

I have a function inside a object that sets up a interval that calls another function but when ever that interval function is called it gives my a error saying Uncaught TypeError: Object [object Window] has no method

here is my code that I’m trying to understand.

function test2() {
this.timer;

this.say = function(){
    console.log("hi");
}

this.start = function() {
    //starts the interval function
    this.timer = setInterval(this.loop, 1000)
}

this.loop = function() {
    //runs every 1 second  
    this.say(); //gives error -- Uncaught TypeError: Object [object Window] has no method 'say'
}
}

var test = new test2();
test.start();

Thank you for your help!

  • 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-06-10T22:41:59+00:00Added an answer on June 10, 2026 at 10:41 pm

    When setInterval() fires, the context is the global context (e.g. window), not your object. To call a method on your object and have the value of this set appropriately in that method call, you need a separate function where you can call the method on your actual object like this:

    this.start = function() {
        //starts the interval function
        var self = this;
        this.timer = setInterval(function() {
            self.loop();
        }, 1000)
    }
    

    FYI, it is very common when using an asynchronous functions like timers or ajax to save the context this into a local variable so it can then be referenced from the embedded callback function even when this is different in the callback function (as in your example). This is a common design pattern.

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

Sidebar

Related Questions

I have this code snippet inside a function that checks if an object exists
I have a function that is often called inside of other functions, and I'd
INTRODUCTION : I have a function(callback) that receives a object as an argument, inside
I have a JSON object that is returned inside a function (see below) and
Inside my Controller i have function that runs after user clicks on item, which
I have a function that takes a By element, By by = By.xpath(xpathString). Inside
I have a function that sets a vector to a string, copies a Sweave
I have have a function in wrote in C++ that calls some functions in
I have an object that has a mathematical function behind it. It seems like
Is there any method that invokes a function but sets the context this to

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.