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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:40:24+00:00 2026-05-14T05:40:24+00:00

I’m having difficulty with Javascript instance variables. I’m trying to make an easy way

  • 0

I’m having difficulty with Javascript instance variables. I’m trying to make an easy way to refresh the chat page, looking for new messages. The AJAX call supplies a mid, or the lowest message id to be returned. This allows the call to only ask for the most recent messages, instead of all of them.

MessageRefresher.prototype._latest_mid;

function MessageRefresher(latest_mid) {
    this._latest_mid = latest_mid; // it thinks `this` refers to the MessageRefresher object
}

MessageRefresher.prototype.refresh = function () {
    refreshMessages(this._latest_mid); // it thinks `this` refers to the window
    this._latest_mid++;
}

function refreshMessages(latest_mid) {
    $.getJSON('API/read_messages', { room_id: $.getUrlVar('key'), mid: latest_mid }, function (messages) {
        for (var i = 0; i < messages[0].length; i++) {
            var newChild = sprintf("<li>%s: %s</li>", messages[1][i], messages[0][i]);
            $("#messages").append(newChild);
        }
    });

    var messageRefresher = new MessageRefresher(0);
    setInterval(messageRefresher.refresh, 1000);

This results in all the messages being printed out, over and over again.

I know it has other bugs, but the main issue I’m trying to work out right now is the use of the instance variable. Or is there another way I should be going about doing this?

UPDATE: Updated code to reflect answers, but it still doesn’t work:

$(document).ready(function () {

    var messageRefresher = new MessageRefresher(0);
    setInterval($.proxy(messageRefresher, "refresh"), 1000);

});

function MessageRefresher(latest_mid) {
    this._latest_mid = latest_mid; // it thinks `this` refers to the MessageRefresher object
}

MessageRefresher.prototype.refresh = function () {
    refreshMessages(this._latest_mid); // it thinks `this` refers to the window
}

function refreshMessages(latest_mid) {
    var refresher = this;
    $.getJSON('API/read_messages', { room_id: $.getUrlVar('key'), mid: latest_mid }, function (messages) {
        if (messages != null) {
            $("#messages-loading-msg").hide();
            for (var i = 0; i < messages[0].length; i++) {
                var newChild = sprintf("<li>%s: %s</li>", messages[1][i], messages[0][i]);
                $("#messages").append(newChild);
            }
        }
        else {
            $("#messages-loading-msg").text("No messages here. Say anything...");
        }

        refresher._latest_mid = messages[2];
    });
    }

What am I still doing wrong? Does this really refer to the messageRefresher, or to the proxy?

UPDATE 2: Getting closer, but still not quite there:

$(document).ready(function () {

    $("#no-js-warning").empty();

    messageRefresher = new MessageRefresher(0);
    setInterval($.proxy(messageRefresher, "refresh"), 1000);

});

function MessageRefresher(latest_mid) {
    this._latest_mid = latest_mid; 
}

MessageRefresher.prototype.refresh = function () {
    var refresher = this;
    $.getJSON('API/read_messages', { room_id: $.getUrlVar('key'), mid: refresher._latest_mid }, function (messages) {
        if (messages != null) {
            $("#messages-loading-msg").hide();
            for (var i = 0; i < messages[0].length; i++) {
                var newChild = sprintf("<li>%s: %s</li>", messages[1][i], messages[0][i]);
                $("#messages").append(newChild);
            }
        }
        else {
            $("#messages-loading-msg").text("No messages here. Say anything...");
        }

        refresher._latest_mid = messages[2]; // break here
    });
}

The above code makes two AJAX calls, then stops. Also, when I break on the “break here” line labeled above, Firebug does not show values for refresher or _latest_mid. Am I passing refresher into the closure correctly?

UPDATE 3: Now it no longer stops making AJAX calls. Perhaps the reason that it sometimes makes multiple calls is that setInterval() fires off multiple requests before the first request has returned and has updated _latest_mid.

How can I use the JQuery Ajax framework to prevent this from happening?

  • 1 1 Answer
  • 1 View
  • 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-14T05:40:24+00:00Added an answer on May 14, 2026 at 5:40 am

    Try this:

    setInterval($.proxy(messageRefresher, "refresh"), 1000);
    

    The problem is that you need to make sure that your object (messageRefresher) is used as the “context” for the call to the “refresh” function. The mere fact that it’s declared as being part of the prototype does not ensure that “this” will always be an instance of the object. In fact, “this” could be pretty much anything.

    The (fairly new) “proxy” method in jQuery returns a reference to a helper function that’ll call your function (“refresh”) with the object you need as the context (that is, “this” inside “refresh”).

    Also that first line, where you just sort-of mention that instance variable, you don’t need that; it doesn’t really do anything.

    edit More explanation.

    The “this” variable inside of any Javascript function, no matter how or where it’s declared, gets its value upon each distinct invocation. It is not a permanent part of the function; in effect, it’s like a somewhat special parameter passed to the function.

    In your case above, you’ve got a line of code inside the function you pass to $.getJSON that refers to “this”. What you want is for that “this” reference to mean your “messageRefresher” object, right? Well, as it stands, there’s no reason for it to have that value.

    I don’t know why you’ve got “refreshMessages” split out like that. Make that be the “refresh” method. Then, save the “this” pointer at the outset, and you’ll be able to refer back from the $.getJSON handler:

    MessageRefresher.prototype.refresh = function () {
      var refresher = this;
      $.getJSON('API/read_messages', { room_id: $.getUrlVar('key'), mid: latest_mid }, function (messages) {
        if (messages.length > 0) {
            $("#messages-loading-msg").empty();
            for (var i = 0; i < messages[0].length; i++) {
                var newChild = sprintf("<li>%s: %s</li>", messages[1][i], messages[0][i]);
                $("#messages").append(newChild);
            }
            refresher._latest_mid = messages[2];
        }
        else {
            $("#messages-loading-msg").text("No messages here. Say anything...");
        }
    
      });
     }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use "00" as the text and select "Match… May 14, 2026 at 9:34 pm
  • Editorial Team
    Editorial Team added an answer If you process credit card numbers in your application, you… May 14, 2026 at 9:34 pm
  • Editorial Team
    Editorial Team added an answer Use the CSS float property: <a href="#" style="float: right;">Link</a> <h1… May 14, 2026 at 9:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.