Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js load multiple images

// Array of images to load.
const images = [
	"path/to/image.extension",
	"/images/someimage.png",
	"./images/anotherimage.jpg",
	"../separatefolder/assets/onelastimage.gif"
];

// Function to load an image.
function loadImage(src) {
	return new Promise(function(res, rej) {
		const image = new Image();
		function loadCallback() {
			image.removeEventListener("load", loadCallback);
			image.removeEventListener("error", errorCallback);
			res(image);
		}
		function errorCallback() {
			image.removeEventListener("load", loadCallback);
			image.removeEventListener("error", errorCallback);
			rej(image);
		}
		image.addEventListener("load", loadCallback);
		image.addEventListener("error", errorCallback);
		image.src = src;
	});
}

// Function to load multiple images.
function loadImages(...srcs) {
	const promises = [];
	for (let i = 0; i < srcs.length; i++) {
		promises.push(loadImage(srcs[i]));
	}
	return Promise.all(promises);
}

// Load the images in the images array and print them to the console.
loadImages(...images).then(console.log);
Comment

create multiple images in js

for(let i = 0; i < 10; i++) {
   const img = document.createElement("img");
   img.src = "https://picsum.photos/200/301";
}
Comment

add multiple images inside the DOM js


function placeImage(x)
{
    var div = document.getElementById("div_picture_right");

    div.innerHTML = ""; // clear images

    for (counter=1;counter<=x;counter++) {
        var imagem=document.createElement("img");
        imagem.src="borboleta/Borboleta"+counter+".png";
        div.appendChild(imagem);
    }
}

window.onload = function() {
    placeImage(48);
};

Comment

add multiple images inside the DOM js

function placeImage(x)
{
    var div = document.getElementById("div_picture_right");

    div.innerHTML = ""; // clear images

    for (counter=1;counter<=x;counter++) {
        var imagem=document.createElement("img");
        imagem.src="borboleta/Borboleta"+counter+".png";
        div.appendChild(imagem);
    }
}

window.onload = function() {
    placeImage(48);
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: requestanimationframe in javascript 
Javascript :: js infinite loop 
Javascript :: how to write unit test cases in react js 
Javascript :: know when recyclerview is on the last item 
Javascript :: javascript callback 
Javascript :: upload file in node 
Javascript :: JS longest word 
Javascript :: random password generator javascript 
Javascript :: js role giver 
Javascript :: react fontawesome exchange icon 
Javascript :: react.createref 
Javascript :: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec. 
Javascript :: javascript async await returns undefined 
Javascript :: discord js people in voice channel 
Javascript :: discord.js embed 
Javascript :: reverse array recursion javascript 
Javascript :: javascript check string sort ascending 
Javascript :: convert javascript date into excel date 
Javascript :: node biology definition 
Javascript :: relation between leaves nodes and internal nodes in binary tree 
Javascript :: javascript add nd to number 
Javascript :: json 
Python :: abc list 
Python :: suppres tensorflow warnings 
Python :: matplotlib dark mode 
Python :: python exception with line number 
Python :: delete column pandas dataframe 
Python :: create requirements.txt conda 
Python :: python random true false 
Python :: How to play music without pygame 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =