After removing a 3D object from the array, THREE.js becomes unresponsive

I am currently developing an online real-time game that involves storing all players and their animation mixers in arrays. However, I have encountered an issue when a player leaves the game. Whenever I attempt to splice them from the player array, the program freezes.

var index = i;
scene.remove(players[index]);
players[index].geometry.dispose();
players[index].material.dispose();
players.splice(index, 1);
interval.splice(index, 1);
mixers.splice(index, 1);
anim.splice(index, 1);
ids.splice(index, 1);

Why does the program freeze when I try splicing these arrays?

Answer №1

After some trial and error, I discovered that attempting to splice 3dObjects from an array within a for loop using three.js was causing issues. To solve this problem, I created a separate function and utilized window.setTimeout() to call it. The end result is a seamless and functional solution.

window.setTimeout(DeleteObject, 10, i);

function DeleteObject(index){
    scene.remove(objects[index]);
    objects[index].geometry.dispose();
    objects[index].geometery = null;
    objects[index].material.dispose();
    objects[index].material = null;
    objects[index].dispose();
    objects[index] = null;
    objects.splice(index, 1);
    intervals.splice(index, 1);
    mixers.splice(index, 1);
    animations.splice(index, 1);
    identifiers.splice(index, 1);
}

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

Strip out the script tag from the data received in the AJAX response

I'm currently developing a single-page application using jQuery. The entire HTML pages are sent to the browser as an AJAX response. $.post(url, function (data) { $("#resp").html(data); $("#resp").find("script").each(function (i) { ...

Antialiasing with Three.js appears to be malfunctioning specifically on iPhone 5 and iPhone 5s devices

As a newbie to three.js, I managed to finish my project successfully. To enhance the graphics quality, I decided to enable antialiasing. renderer = new THREE.WebGLRenderer( { antialias: true } ); Unfortunately, after enabling antialiasing on iPhone 5 and ...

Launching a program through a web browser - a step-by-step guide

I am looking to create a specific sequence of events: A web browser opens, and a user logs in (the exact action is not crucial) The user is then redirected to a new page where another program should automatically open This process is similar to what happ ...

Finding the current div based on the scrolling position can be achieved without the use of jQuery

Seeking assistance with a coding challenge. I have a solution in jQuery, but we require the solution in plain JavaScript or Angular. It seems like something is missing. Take a look at this fiddle. The objective is to highlight the corresponding left panel ...

Maintain the state of the previous page in Vue to ensure continuity

Currently in the process of creating a small PWA to simulate an Android app, I have discovered Vuejs. However, I encountered an issue that has proven difficult to resolve. While scrolling through lists on the homepage for movies, TV shows, or news, clicki ...

Utilizing VueJS to choose an item from a list and exhibit it using a function

My goal is to retrieve the element I click on, displayed in a list, and then print this element using a method within my Vue instance through the mail-list tag in index.html. I have a Vue component that iterates over a JSON object and only displays two at ...

Concealing the clicked HTML element once the AJAX request is successful

Just starting out in Web Development and creating a mock twitter application. I'm looking to eliminate the tweet box once the delete button is clicked (only if it's actually deleted on the backend) Utilizing django templating to iterate through ...

Tips and tricks for retaining the collapsed state upon reloading Bootstrap 5

Just diving into the world of bootstrap and javascript. How do I save the collapsed state to make sure it stays even after refreshing the page? <p> <button class="btn btn-primary" type="button" data-bs-toggle="collapse&q ...

Identify duplicate values within an array and remove them based on certain conditions

Here is an example of a multidimensional array: $orders = array( array( 'id' => '123', 'name' => 'John', 'lastname'=>'Carter', 'rate' => '1.0' ...

Getting the result from a JavaScript request, whether it's synchronous or asynchronous

This code snippet involves a function that starts with a synchronous comparison test == 0. If the comparison is true, it returns one piece of content; however, if it's not, an asynchronous request is made. The goal here is for the latter part to retur ...

What is the best way to incorporate JQuery into html content retrieved through an ajax request?

In my jsp file, there is a div structured like this : <div id="response" class="response"></div> After triggering an ajax call to a servlet, the content of this div gets updated with the following elements : <div id="response" class="res ...

What is the best way to extract the value from a specific field in a SQL database?

I am trying to extract only the value of a field from the database. First, I count the number of rows in my database with this line: var num = await pool.query('SELECT count(*) FROM links WHERE user_id=?',[req.user.id]); However, the result is ...

Generating a MySQL query to create a table in JSON structure using JavaScript

Has anyone tried creating a new MySQL table using JSON format? I have a test JSON file and I want to convert it into a new database table. [ { "name": "work_schedules", "columns": { "test": "SMALLINT", "test2": ...

Ways to evaluate the properties of two objects in Angular

On clicking the add new button in my form, I am dynamically adding a new row. The newly entered data is stored in the oldData array as an object. I need to compare all values in the newData object with the objects in the oldData array. If there already ex ...

How to retrieve the value of a nested checkbox in Angular using dynamic methods

I successfully developed dynamic nested checkboxes in Angular and now I am looking to retrieve the values of the selected checkboxes. However, I encountered an issue with the JSON structure needed to implement this functionality. https://i.stack.imgur.com ...

Tips for Implementing a "Please Hold On" Progress Bar in ASP.NET

I have a master page named siteMaster.master, an aspx page called submission.aspx, and a user control named attachment.ascx. The script manager is included in my master page. The submission page inherits the master page and registers the user control attac ...

Leveraging the Wikia API

I've been attempting to utilize the X-men API on Wikia in order to gather the name and image of each character for use in a single page application using JavaScript. You can find the link to the wiki page here: Despite my efforts, I am struggling to ...

Modifying the width of a jQuery UI dialog from within the dialog box itself

I have designed an ASP.Net web page with a div and iFrame to display another page within it. Now, I am wondering if there is a way to change the width of the dialog from a button inside the dialog itself... Is this achievable? Thank you. ...

Experiencing a crash immediately following the implementation of the operator new[] in C++

I'm dealing with a memory allocation issue in my code. Specifically, when working with arrays v1 and v2 in my Vector class, I encounter problems with memory allocation during the copying process. How can I resolve this issue? Below is the relevant cod ...

Reposition checkbox within dropdown list

To create a dropdown menu with textboxes, I implemented the code found in this Stack Overflow answer: Now, I am looking to reposition the dropdown menu to align with a specific piece of text, preferably to the right side of the 'Textpiece'. I at ...