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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:24:23+00:00 2026-06-17T20:24:23+00:00

I’ve been trying to add data into my array for sometime now and it

  • 0

I’ve been trying to add data into my array for sometime now and it doesn’t work. I have the following code:

function OBJMesh(file)
{
    this.modelVertex = [];
    this.modelColor = [];
    var that = this;
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {

        if(rawFile.readyState == 4)
        {
            if(rawFile.status === 200 || rawFile.status === 0)
            {
                var allText = rawFile.responseText;
                var lines = allText.split("\n");


                for(var i = 0; i < lines.length; i ++)
                {
                    var lineData = lines[i];
                    var lineString = lineData.split(" ");

                    if(lineString[0] === "v")
                    {

                        var x = parseFloat(lineString[1]);
                        var y = parseFloat(lineString[2]);
                        var z = parseFloat(lineString[3]);

                        /*
                        this.modelVertex.push(x);
                        this.modelVertex.push(y);
                        this.modelVertex.push(z);

                        this.modelColor.push(0.0);
                        this.modelColor.push(0.0);
                        this.modelColor.push(0.0);
                        this.modelColor.push(1.0);
                        */

                        that.modelVertex.push(10.0);

                        //document.getElementById("textSection").innerHTML = "testing";
                    }

                }
            }
        }
    }

    rawFile.send();

}

OBJMesh.prototype.getModelVertex = function ()
{
    return this.modelVertex;
};

OBJMesh.prototype.getModelColor = function ()
{
    return this.modelColor;
};

If I comment out the this.modelVertex.push(10.0); it gets pass the error and prints out “testing”. But if I uncomment it, it gets stuck there and won’t print anything out. Why is it doing this? how can I solve it so it actually pushes the given data to the this.modelVertex array?

Many Thanks

Edit: I have edited my code after dystroy told me what to do and it do work when I try to print the values in the OBJMesh constructor (shown above), but when I try to do this by creating the object in my main function (shown below) it doesn’t print anything.

var cubeModel;

function main()
{
    cubeModel = new OBJMesh("file:///Users/Danny/Desktop/3DHTMLGame%202/cube.obj");

    document.getElementById("textSection").innerHTML = cubeModel.getModelVertex();
}
  • 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-17T20:24:25+00:00Added an answer on June 17, 2026 at 8:24 pm

    this isn’t your new instance of OBJMesh in the callback but the XMLHttpRequest.

    Start by referencing the desired object just before defining the callback :

    var that = this;
    rawFile.onreadystatechange = function ()
    

    then use it :

    that.modelVertex.push(10.0);
    

    Answer to the question in your edit :

    Your constructor contains an asynchronous request. Which means your array isn’t available immediately but later.

    A solution would be to pass a callback to the constructor :

    function OBJMesh(file, doAfterInit) {
        this.modelVertex = [];
        this.modelColor = [];
        var that = this;
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", file, true);
        rawFile.onreadystatechange = function () {
            if(rawFile.readyState == 4)
            {
                if(rawFile.status === 200 || rawFile.status === 0)
                {
                    var allText = rawFile.responseText;
                    var lines = allText.split("\n");
                   for(var i = 0; i < lines.length; i ++)
                    {
                        var lineData = lines[i];
                        var lineString = lineData.split(" ");
                        if(lineString[0] === "v"){
                            that.modelVertex.push(10.0);
                            if (doAfterInit) doAfterInit();
                        }
                    }
                }
            }
        }
        rawFile.send();
    }
    
    ...
    
    
    cubeModel = new OBJMesh("file:///Users/Danny/Desktop/3DHTMLGame%202/cube.obj", function() {
        document.getElementById("textSection").innerHTML = cubeModel.getModelVertex();
    });
    

    But having a class here doesn’t look like a smart idea.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
I have been unable to fix a problem with Java Unicode and encoding. The

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.