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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:50:07+00:00 2026-05-22T01:50:07+00:00

So i am trying to learn object oriented programming in javascript. function doStock() {

  • 0

So i am trying to learn object oriented programming in javascript.

function doStock() {    //my class
var that = this;
var nAntiFreeze = null;   // timeout ID
var getContent = function(oInPageContainer) {  
    GM_log('Antifreeze, before clear ' +nAntiFreeze);
    //clearTimeout(nAntiFreeze);
    GM_log('Antifreeze, after clear ' +nAntiFreeze);
};

return {
    sLink : "",
    oList : "",
    sSplitOperator : ";",
    reset : function() {
        this.sLink = '';
        this.oList = '';
        this.sSplitOperator = ';';
        nAntiFreeze = null;
    },
    loadPage : function() {
        if (this.sLink.length == 0) return;
        if (this.oList.length == 0) return;
        nAntiFreeze = setTimeout(function(){GM_log(that); that.loadPage();},30000);
        GM_log('antifreeze ' + nAntiFreeze);
        getPageAsync2(this.sLink,false,getContent);  //GM_xmlhttprequest
    }
}

};

My script runs on GreaseMonkey in FireFox 4. In my code i am using the above function/class to make an object as follows.

var oStocker = new doStock();
oStocker.sLink = 'www.somepage.com';
oStocker.oList = 'some list, may be a string line or the array object';
oStocker.loadPage();

getPageAsync2 function calls GM_xmlhttprequest and then returns the result page contents inside a div container to the callback function.

First, general question: value of nAntiFreeze does not get reset to null or anything after i call clearTimeOut function. Is this normal?

Second question: why when the timeout runs out, i get the error that.loadPage() is not a function? GM_log(that) tells me [object Object].

A person on this question was able to make it work by using var that = this. But why is it not working for me?
Custom Object calling Methods with setTimeout loses scope

EDIT: Third question: What happens if i create a million objects. Will browser get rid of them when they are done working? Because i sure am unable to free them as this object uses asynchronous ajax calls, which means that i can’t do

var oStocker = new doStock();
oStocker.loadPage();
oStocker = null;

The oStocker = null will be called before my object even finished working.

Thanks

  • 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-22T01:50:08+00:00Added an answer on May 22, 2026 at 1:50 am

    Firstly, nAntiFreeze is a primitive value returned by setTimeout. You pass that value back to clearTimtout so it knows which timeout to clear. Calling clearTimeout doesn’t affect the value of nAntiFreeze.

    Secondly, that.loadPage is undefined because that references this when doStock() is called (where it is called as a constructor with new, it references a new Object). But your function doesn’t return that object (i.e. the constructor’s this), it returns the object after return that the loadPage() function is a method of. In other words, you are referencing the wrong object.

    When you call oStocker.loadPage(), its this keyword references the oStocker object, but the function passed to setTimeout is referencing that, which has a closure to the constructor’s this.

    The following should work:

    loadPage : function() {
    
        // Declare that here
        var that = this;
    
        if (this.sLink.length == 0) return;
        if (this.oList.length == 0) return;
    
        // If called as oStocker.loadPage(), this (and that) is
        // the oStocker object.
        nAntiFreeze = setTimeout(function(){GM_log(that); that.loadPage();},30000);
        GM_log('antifreeze ' + nAntiFreeze);
        getPageAsync2(this.sLink,false,getContent);  //GM_xmlhttprequest
    }
    

    There isn’t much point using a constructor that doesn’t return its this, you can use Richard Cornford’s module pattern and use closures for inheritance.

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

Sidebar

Related Questions

I'm trying to learn object oriented programming more clearer by creating a database class
I think object oriented programming is overrated, however I want to learn this paradigm.
I am trying to learn object oriented programming in the context of Java. I
I'm trying to learn object oriented PHP by porting some of the simple exercises
I've been trying to learn the object oriented side of PHP, and was wondering:
I've recently been trying to learn the Object-Oriented aspects of F#, and have become
I'm trying to learn C++ so forgive me if this question demonstrates a lack
In trying to learn how to create objects in ActionScript, I have had no
Trying to learn a bit of CSS and I want a horizontal navbar and
I'm trying to learn C. As a C# developer, my IDE is Visual Studio.

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.