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

  • SEARCH
  • Home
  • 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 6956499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:40+00:00 2026-05-27T14:52:40+00:00

In my application I have an object with several properties that get set in

  • 0

In my application I have an object with several properties that get set in various places in the application.

In one of my prototype functions I have a function that runs in intervals to update a timer, and in that function the property (this.)theTime should be set. The problem is that this doesn’t happen, and I guess the reason is that this.theTime points to the function itself instead of the object.

Below is two versions of my code, and neither of them works. Any tips for me?

// 1.
function changeTime() {
    this.theTime = setTime(time);
    time.setSeconds(time.getSeconds()+1);
    p1.html(this.theTime);
}
interval = setInterval(changeTime(), 1000 );

// 2.
function changeTime(theTime) {
    theTime = setTime(time);
    time.setSeconds(time.getSeconds()+1);
    p1.html(theTime);
}
interval = setInterval( function() { changeTime(this.theTime); }, 1000 );

…

Too make it more clear, the function above updates a timer (eg. 00:00:01 -> 00:00:02) every second, and I want this.theTime to be updated with the time.

When the timer stops (which happens in another prototype function) I want to be able to see what time the timer stopped on, but as it is now this.theTime is the default value, which means that the function above doesn’t update the objects property. Instead this.theTime in the function above must be a local variable.

NOTE: setTime() is another function that exists in the same prototype function as the function above.

  • 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-27T14:52:41+00:00Added an answer on May 27, 2026 at 2:52 pm

    Well when you use this in some function this is referencing to the object which actually the function is. Here:

    function myF() {
        this.var = 'hey';
    }
    

    You can reach var using this (myF as a constructor function):

    var obj = new myF();
    alert(obj.var);
    

    Or here:

    function myF2() {
        if (typeof this.var === 'undefined') {
            this.var = 0;
        } else {
            this.var += 1;
        }
        alert(this.var);
    }
    

    Here var again is a property of myF2 (which as I said is not just a function because in JavaScript functions are objects).
    Each time you call myF2 this.var is going to be incremented and alerted (just in the first call it’s going to be initialized).

    In the second function (anonymous function using in the second setInterval) you’re doing the same.

    One solution is to make theTime global in both cases so you don’t need to use:

    this.theTime
    

    So the result can be something like this:

    var theTime = 0, interval;
    function changeTime() {
        theTime += 1;
        document.body.innerHTML = theTime;
        setInterval
    }
    
    interval = setInterval(changeTime, 1000 );
    

    http://jsfiddle.net/u3EuC/

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

Sidebar

Related Questions

I have an application that has several objects (about 50 so far, but growing).
I am building a web application and have been told that using object oriented
I'm working on an iPhone application. I have an object of class Row that
I have an AJAX application that downloads a JSON object and uses the data
I have an ASP.NET web application that takes user input across several forms. Sort
I have a simple java object with several date properties and I always seem
I have an application that performs a very sequential set of discrete tasks. My
On one of the edit views of my Grails application I have several chained
I have an application that uses several different Java classes and would like to
I have an object created in a host application and can access it remotely

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.