inept java script circle

Each image in the array is superimposed on one another to form the final picture. However, a problem arises when all the images are loaded - the loop continues to run making it extremely difficult to scroll or execute any other animations.

function map(id){
var img_elem = document.getElementById("img_elem");
var ctx = document.getElementById('canvas').getContext('2d');
var name = <?php echo json_encode($sorted_array);?>;
var elements=<?php echo $filecount?>;
var image= [];
for(i=0;i<elements;i++){
image[i]= new Image();
image[i].src = "images/"+name[i];
image[i].setAttribute("class","container");
image[i].setAttribute("style","position:absolute");
ctx.drawImage(image[i],1,1);
alert(image[i].src);
}
}

Answer №1

One of the main reasons why this process may be running slowly is due to the frequent appending of elements to the DOM, which can trigger the browser to constantly redraw the page.

To improve performance, consider consolidating your changes and only adding them once, such as shown in the example below:

let buffer = [];
for(let i=0; i<elements; i++){
    buffer.push("<img ",
        "src='images/", name[i],"' ",
        "class='container' ",
        "style='position:absolute' ",
        "id='", name[i],"'/>");
}
document.getElementById(id).innerHTML = buffer.join("");

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

Is your YQL JSON script failing to provide the expected output?

Here is a script that I have copied almost directly from this. Why is it that the code below does not seem to return anything? ajax.html: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html dir="lt ...

Toggle the visibility of radio buttons by displaying a hidden div class instead. When the div is clicked, automatically select

I'm attempting to make this functionality work differently. Instead of displaying the standard radio button, or replacing it with an image, I want to use a styled div as a button. When hovered over, the class should change, and when clicked, the class ...

Merge objects based on specific property within an array of objects

Is there a way to merge objects based on one property and also add missing Days names in the output? Consider this example: var array = [ { "heure1": "14:00", "heure2": "17:00", "day&q ...

Attempting the transformation of jQuery ajax requests to Angular's http service

I am looking to transition my existing mobile application from using jquery ajax to angularjs for handling user authentication with server-side validation. Here is the original jquery ajax code: function validateStaffUser(username, password) { var re ...

Problem with sortable columns in DataTables

$(document).ready(function () { var dt = $('#example').DataTable({ "processing": true, "serverSide": true, "ajax": "api.php?t=clients", "columns": [{ "className": "details-control", ...

Tips for extracting value from a chosen input field without relying on its unique identifier

I am working on a simple code to help guess notes by ear. The concept involves tabs with empty input fields where numbers need to be entered based on a specific melody for the guitar fretboard. One button reveals the first note, while another button checks ...

Angular ng-click feature for a dropdown option element

Check out this jsfiddle link: http://jsfiddle.net/cuycbxxp/ Within this dropdown selection, you can choose between names and numbers. Once you select a name and then select a number from the dropdown, a function is executed and an output is displayed in ...

Node.js Promise function halting execution prior to returning

How can we adjust the code provided below to allow the getUser(theToken) promise function to successfully return its internally generated valid value without freezing? app.get('/auth/provider/callback', function(req, res) { var queryData = u ...

PHP arranges an array of dates in a formatted manner

I am currently working on a function to extract and format dates from nested arrays within the same array. While I can successfully locate and display the values in the array, I am facing difficulty in formatting the date as per my requirements for output. ...

Using AngularJS to dynamically change the background color of table rows based on the values stored in a data structure

I am looking to implement dynamic color backgrounds for table rows based on the state of a specific data structure Below is my attempt: <tr data-ng-repeat="shipping in shippings" data-ng-init="shippingIndex = $index" data-ng-class=& ...

Fixing a compilation error when attempting to paste text into a file with Selenium Webdriver

I am new to using Selenium webdrivers and encountering a compilation error with the code below. Can anyone provide some assistance? My goal is to save a message into a file rather than displaying it on the console. testResultFile="C:\&b ...

Looking to show an image when a checkbox is ticked and hide it when it is unticked in HTML?

How do I make a specific image appear when a user clicks on a checkbox? I'm unsure if I should use jQuery, Ajax, or another method. What is the best approach for this task? Your assistance would be greatly appreciated. I have successfully created the ...

What causes a CSS class to not be applied when using JavaScript onload?

I've been attempting to add a class to an element after the page has loaded in order to change its height and opacity through a transition effect, but so far, I've been unsuccessful. This is how my HTML file looks: <head> <script> ...

Encountering undefined JavaScript functions when called from HTML

I had most of these functions working perfectly, but after restarting my program, I'm now encountering issues with undefined functions in Chrome. I can't pinpoint the exact problem, and even though I've checked through Eclipse, I still can&a ...

The post request fails to send certain variables

I'm experiencing an issue with a form that has rows structured like this: <div class="row unit" onmouseover="this.style.background = '#eeeeee';" onmouseout="this.style.background = 'white';" onclick="if(event.srcElement.nodeNam ...

JavaScript can be used to target the textbox and give it focus

I am currently working on a code that should focus on textboxes based on their arrangement. However, I am facing an issue where the code is directing the focus to the third textbox. Instead, I want it to focus on the textbox based on its position in the ...

requesting data and receiving a promise object

I developed a function called getCartItems that invokes getSingleItems with the ID as an argument. When I log the JSON result in getSingleItem, it correctly displays the product object. However, when I try to access the function call value, I am getting a ...

Running Selenium tests are not able to initiate Javascript ajax requests

I'm currently troubleshooting a scenario using Selenium that involves the following steps: - When a user inputs text into a textarea, an ajax request is sent, which then adds the text to the database (implemented using django and processed in view.py) ...

Exploring the functionality of textareas within iframes on iPad

There is a known issue where textareas and inputs do not function properly on iPad Safari when placed inside an iframe. For more information, visit: This bug can easily be reproduced on iOS 7, but seems to work fine on iOS 8. Despite this issue, I am in ...

Utilize a pre-defined layout in a Vue component that is sent as a property

As a complete beginner, I kindly ask for your patience as I navigate through the coding basics. I am looking to utilize a template specified in the prop. The template is located within the DOM. My intention for approaching it this way is to reuse the comp ...