Managing images in JavaScript while conserving memory

Welcome

I am embarking on a project to construct a webpage using HTML, CSS, JavaScript (jQuery), and nearly 1000 images. The length of the HTML page is substantial, around 5000px.

My vision involves creating a captivating visual experience for users as they scroll through the page. I want the images to animate like a flipbook in the background, synchronizing with the user's scrolling progress. For instance, if the total image count is 1000 and the user has scrolled 32% down the page, they should be viewing the 320th image. Upon reaching the end of the page, they will see the final 1000th image.

How can I tackle this efficiently? Each image file size averages about 150kb, and it is crucial for me to preserve their quality without further compression.

Exploration So Far

  1. Maintaining all images in memory constantly

    New images are loaded as the user scrolls, while previous ones are hidden with display: none;. Although functional, this method becomes progressively sluggish with extended scrolling.

  2. Limited image retention in memory

    This method involves loading the current image along with 20 preceding and following images. Images beyond this range are "removed from memory". Despite efforts, performance issues persist, possibly due to inadequate memory management practices.

Inquiry 1

What techniques or strategies would you recommend for optimizing this project?

Although option 2 seems promising, I am open to alternative suggestions that could enhance efficiency.

Inquiry 2

How can I effectively remove images not just from the DOM but also from memory?

To ensure the success of this project, proper handling of image removal seems necessary.

Current Strategy

Here's an overview of my current approach:

var imageNum = -The current image number to be displayed-;
var flip = -the container element housing all images-;

flip.children().each(function(i, e) { 
    var jE = $(e);
    var childInd = parseInt(jE.data('page-number'));

    if (childInd < imageNum - 20 || childInd > imageNum + 20) {
        jE.removeAttr('src');
        jE.remove();
        jE = null;
        e = null;
    }
});

The section above raises concerns regarding proper memory release:

jE.removeAttr('src');
jE.remove();
jE[0] = null;
e = null;

These lines were composed after consulting various sources. Since no event handlers are attached to the images, this aspect is likely not problematic.

How can I ensure effective garbage collection to free up memory occupied by these images? Is my current procedure adequate to facilitate memory liberation?

Answer №1

By utilizing image sprites, you have the ability to create one composite image containing multiple individual images, allowing you to retrieve each image using specified coordinates.

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

Assigning values from JSON data in jQuery

I'm facing an issue with a php query that outputs json containing a single value. The json data format is as follows: {"value":53}. I've been attempting to set this value as a variable in a jquery function, but have not been successful so far des ...

Guide to showcasing user input using a Javascript function

I need assistance to show the user input via the submit button. Users will enter tool types and the first five inputs will be displayed in list items (li). Once the limit of 5 tools is reached, a message 'Thanks for your suggestions' should appea ...

Why is the size of my array shrinking with every iteration of the for-loop in JavaScript?

I am struggling to change the classname of three elements that share the same classname. Unfortunately, as I loop through my array, it seems to decrease in size with each iteration, preventing me from successfully changing all three elements. Any advice or ...

Tips for building a custom error handling function specifically designed for parsing and handling errors in

Is there a way to reduce redundancy in my code, such as: let name = ""; try { name = data[" "][0][" "][0][" "][0][" "][1][" "][0][" "][1]["C"]; } catch (error) { if (error instanceof TypeError) { console.log("name TypeError"); } } I have consid ...

Using JavaScript to toggle the visibility of grids depending on the radio button choice and then triggering this action by clicking on the search button

As a newcomer to AngularJS development, I am encountering some challenges while attempting to implement the following scenario. Any suggestions or guidance would be greatly appreciated. I aim to showcase either one or two Angular UI grids based on the rad ...

Utilizing App Script for Filtering Data with Multiple Criteria

Having trouble transferring data from a data sheet to my report sheet using multiple criteria for matching. I wrote some code that worked, but it's returning all data instead of filtering by criteria. I want the function to search for column criteria ...

Restangular is throwing an error because it cannot find the reference for _

I encountered an issue with Restangular where I'm getting the error message: Uncaught ReferenceError: _ is not defined from restangular when trying to use it. Code Snippet: <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angula ...

Accessing HTML elements that are created dynamically in AngularJS

I am facing an issue where I cannot access a function within a newly created DOM element. Despite my best efforts, I can't seem to figure out what is causing this problem. $scope.createCustomHTMLContent = function(img, evtTime, cmname, evt, cust, ser ...

Is it possible to trigger a JavaScript function and an AJAX command with just one button click?

I'm looking to achieve a specific functionality using one button within a form. The requirements are as follows: 1) When the button is clicked, it should trigger a JavaScript function that performs animations such as fadeout and fadein. 2) Following ...

A revolutionary approach - Empowering websites with individual scrolling panels

I have a unique design layout that includes a top navigation, side navigation, and content. Both the side navigation and the content sections are taller than the page. My goal is to eliminate the need for a scrollbar for the entire page, but still mainta ...

Guide to vertically centering Font Awesome icons inside a circular div

Is there a way to perfectly center align font awesome icons vertically? I have tried using the line-height property when text is present, and even setting the line-height equal to the height of the div. Attempts with display:inline-block and vertical-alig ...

Obtain the URL value using jQuery and place it inside a <span> element

How can I insert a href value into the span tag like this? <p class="name"> <a download="adja-lo.pdf" title="adja-lo.pdf" href="http://localhost/MatrixDRSnews/apps/Matrix/server/php/files/adja-lo.pdf">adja-lo.pdf</a> </p> ...

Pause server processing temporarily on datatables

I currently have a datatable that is populated with server-side data, which is functioning correctly. However, I now want to update the data rows by listening to notifications from AWS SQS. When a new row of data is received and added to the table API, cal ...

Ways to access the values of checkboxes that are initially checked by default

Recently, I was working on a project that involved multiple checkboxes. My goal was to have the checkboxes pre-checked with values in the form (using reactive form). Users should be able to unselect the boxes as they wish and the data would be stored accor ...

Embeddable javascript code can be enhanced by incorporating references into the header

I have developed a basic calculator application using HTML, JavaScript, JQuery, and CSS. My intention is to allow others to embed this calculator app on their own web pages by utilizing a file named generate.js. Within this file, there are several documen ...

Invalid PDF File - Unable to Complete Download via $http

I'm facing an issue while attempting to download a PDF file using the $http service in AngularJS. Upon trying to open the downloaded file, I encounter an error message stating "Invalid Color Space" and the page appears blank. To troubleshoot this pr ...

Avoid the resetting of chosen value following a POST or submission

I am currently working on creating a "web dashboard" that requires a year_from parameter from an HTML form. Despite setting up my Flask backend to return data within this specified range, I am encountering a problem where every time I hit submit, the selec ...

Using async/await with Axios to send data in Vue.js results in different data being sent compared to using Postman

I am encountering an issue while trying to create data using Vue.js. The backend seems unable to read the data properly and just sends "undefined" to the database. However, when I try to create data using Postman, the backend is able to read the data witho ...

Troubleshooting: Difficulty Reading .val() on Elements Selected by Class in jQuery.fn.init(9)

I am facing an issue while trying to retrieve all elements with a specific class name using the code below: productPrices = $('.product-price'); Instead of getting individual values, I am getting the following output: jQuery.fn.init(9) [div. ...

What's the preferred formatting for a Jade Layout?

I've been struggling to convert this HTML code into Jade: <div class="textItem"> <div class="avatar"> <img src="http://wowthemes.net/demo/biscaya/img/demo/avatar.jpg" alt="avatar"> </div> "How many times ha ...