Loading Sequence in Three.js

I'm facing an issue with my Three.js Json-Loader. I have a set of objects whose paths are stored in an array. My goal is to load them and organize them into a list for easy selection. However, the order in which they load differs from their arrangement in the array because of varying sizes – smaller objects are loaded first and larger ones last. As a result, after loading them, I am unable to determine the name of the object (which is represented by its path).
Here's my current code:

for(var j=0;j<21;j++){
        var path = objPath[j];
        loader.load( path, function( geometry ) { save(geometry, path); } );
    }


As per this code snippet, the path passed to the save method always corresponds to the last path in the array (objPath[20]) due to the for-loop executing faster than the loading method itself. How can I ensure that the correct path is retained?

Answer №1

One common mistake with JavaScript closure rules is causing you to be bitten by a specific issue. You can find more information on this topic at this link. Essentially, in order to ensure the correct path is passed to the save function, it is necessary to enclose it within a helper function factory. The code snippet below demonstrates how to achieve this:

function saveHelper(path) {
    return function(geometry) {
        save(geometry, path);
    }
}

for(var j=0;j<21;j++){
    var path = objPath[j];
    loader.load( path, saveHelper(path) );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize auto-suggestion feature for populating dynamically created input fields with information sourced from the

I have searched through numerous questions on stackoverflow related to my issue, but I was unable to apply any of them to solve my problem. Therefore, I am providing the files that I have been working with. I have successfully implemented autocomplete fe ...

Is it possible to identify disconnected geometric shapes when importing using Three.js?

I have been working on importing .3DS models into Blender 2.72b and exporting them using the Three.js import/export addon. These models consist of multiple geometry 'islands', which are separate groups of connected faces and vertices, each with i ...

"Converting web-based SVG graphics for high-quality print output

In the process of developing an innovative online restaurant menu editor, I have constructed a template utilizing SVG and a jpeg image as the background. Users can easily modify the text using SVG elements overlaying the template image. I have mastered th ...

Converting form input to JSON and then parsing it into an object

I'm currently working on converting form data to update a JSON object upon submission. Here's my progress so far: var app = { slidePreviews: "", selectedTheme: "", slideDuration: 7000, slides: [] }; $(document).ready(function() ...

Real-world demonstration of multiple screens using three.js for an immersive virtual experience

Check out these awesome examples of multiscreen capabilities: webgl_multiple_canvases_circle webgl_multiple_canvases_complex webgl_multiple_canvases_grid These examples even reference the Google Liquid Galaxy project: liquidGalaxy I am exploring how t ...

Navigating to a Different Vue or Page with Vue Form Wizard

I'm currently exploring VUE JS and trying to figure out how to redirect to another vue or page after submitting a form. I've been working with the sample code provided at https://stackblitz.com/edit/vue3-form-wizard-demo?file=src%2FApp.vue. Whil ...

Step-by-step guide to uploading text and image simultaneously through AJAX

I encountered a challenge while attempting to create a signup form that requires user data and an image upload. Despite my efforts, I couldn't achieve the desired outcome with the provided code snippet. I've explored similar questions but none ha ...

Loop through the array and print out only the last item

I am struggling to get my for loop to print out every item in the array, instead of just the last item. Can't seem to figure out where I'm going wrong: var patients = ["Julia", "Kelly", "Thomas", "Clare"]; function lineOfPatients(line) { if (! ...

Locate and extract the JSON object using the specified key-value pair

Trying to extract a specific object from a complex json file, noting the key, and then returning a new json poses a challenge. I am using expressjs as the backend for this task. Sample.json '[{"ServID":"Rai 1","Parametri" ...

Displaying column values in Vuetify Table based on a condition

https://i.stack.imgur.com/wK9uU.png I'm working with a Vuetify table that has a column for URLs. I need to implement logic to display either the URL or the URL Group name based on properties in my rules array. If rules[i].urlGroup is not empty, then ...

Ensure that you call setState prior to invoking any other functions

Is there a way to ensure that the setSearchedMovie function completes before executing the fetchSearchedMovie(searchedMovie) function? const { updateMovies, searchedMovie, setSearchedMovie } = useContext(MoviesContext); const fetchMoviesList = (ev ...

JavaScript string manipulation function

Hey everyone, I need help finding a one-line solution in JavaScript to determine if a string contains a semicolon without using a loop. If anyone knows how to do this, please share your knowledge! ...

Tips for resolving the <!--bindings={ "ng-reflect-ng-for-of": "" }--> bug

Just starting out with Angular and I am having some issues with event binding and property binding in my game-control component. I have a button that, when clicked, adds numbers to an array using setInterval method. I am also passing data between compone ...

Sorting the output with gulp and main-bower-files (gulp-order is not functioning)

Hello, I'm a Java engineer diving into the world of Javascript for the first time. Apologies in advance if my information is lacking or incorrect! I am currently working on a gulp build script and utilizing bower to manage dependencies for my JS fron ...

Having trouble navigating the Request and Response handling in Expressjs/Nodejs?

As I continue to delve deeper into this code, confusion seems to cloud my understanding. Here is the provided source: var express = require('express') , http = require('http') , server = express() ; var home = require('./ro ...

Generating default settings for various highcharts

I am utilizing multiple highcharts on my webpage and looking for a way to streamline the process by creating default options that can be reused, eliminating the need for repetitive code. Code: Chart#1 Highcharts.chart('chart1', { ...

Understanding the inner workings of a Mongoose model without the need for

server.js process.env.NODE_ENV=process.env.NODE_ENV || 'development'; var mongoose=require('./config/mongoose'); express=require('./config/express'); var db=mongoose(); var app=express(); app.listen(3000,function(){ ...

Unable to back out of side navigation

My side navigation menu is giving me some trouble. It opens up just fine when I click the button, but it won't close back up when I click it again. The button is supposed to toggle between opening and closing the side navigation. I've looked at ...

Iframe Loading at Lightning Speed

I have set up a script to load my iframe, but I noticed that the script loads the iframe content too quickly. Is there a way for me to configure it to preload all CSS images and content in the background before showing the full iframe content? Here is my ...

Verify the values in the dropdown menu and then cross-reference them with the user's input

Is there a way to verify if the textfield, newTeamName, is already included in a list of teamnames that are stored within a select box? I seem to be encountering issues with my code - can you point out what might be the problem? Just to note, there are no ...