Note that there are some explanatory texts on larger screens.

plurals
  1. POReaching an Object's Property
    text
    copied!<p>In my application I have an object with several properties that get set in various places in the application.</p> <p>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 <code>this.theTime</code> points to the function itself instead of the object.</p> <p>Below is two versions of my code, and neither of them works. Any tips for me?</p> <pre><code>// 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 ); </code></pre> <p>...</p> <p>Too make it more clear, the function above updates a timer (eg. <code>00:00:01</code> -> <code>00:00:02</code>) every second, and I want <code>this.theTime</code> to be updated with the time.</p> <p>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 <code>this.theTime</code> is the default value, which means that the function above doesn't update the objects property. Instead <code>this.theTime</code> in the function above must be a local variable.</p> <p>NOTE: <code>setTime()</code> is another function that exists in the same prototype function as the function above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload