Search
 
SCRIPT & CODE EXAMPLE
 

HTML

js wait for images to load

// 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

javascript wait for image to load

<!-- new Image(); -->
<script>
	const image = new Image();
	// make sure onload is set before src is set
	image.onload = function() {
		console.log("Image loaded:", image.src);
	};
	image.src = "image url";
</script>
<!-- <img> -->
<img id="img" src="image url"></img>
<script>
	const img = document.getElementById("img");
	img.onload = function() {
		 console.log("Image loaded:", img.src);
	};
</script>
Comment

javascript wait image load complete

const loadImage = src =>
  new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.onerror = reject;
    img.src = src;
  })  
;

loadImage("http://placekitten.com/90/100").then(image => 
  console.log(image, `
loaded? ${image.complete}`)
);
Kod parçacığını çalıştır
Comment

javascript wait image load complete

const loadImage = src =>
  new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = () => resolve(img);
    img.onerror = reject;
    img.src = src;
  })  
;

const imageUrls = [
  "http://placekitten.com/85/150",
  "http://placekitten.com/85/130",
  "http://placekitten.com/85/110",
];
Promise.all(imageUrls.map(loadImage)).then(images => {
  const canvas = document.createElement("canvas");
  document.body.appendChild(canvas);
  const ctx = canvas.getContext("2d");
  images.forEach((image, i) =>
    ctx.drawImage(image, i * 90, 0, image.width, image.height)
  );
});
Kod parçacığını çalıştır
Comment

PREVIOUS NEXT
Code Example
Html :: google no translate meta tag 
Html :: links in html 
Html :: &nbsp html 
Html :: name attribute in html 
Html :: typo3 symlinks 
Html :: clipboarddata.getdata html 
Html :: html data-target modal 
Html :: button with icon android 
Html :: html default file name a tag 
Html :: telegram href link code 
Html :: hypertext markup language 
Html :: ng for 
Html :: display date on html 
Html :: how to disable tabindex 
Html :: html vertical text in table cell 
Html :: subscript html 
Html :: wikipedia api url 
Html :: boostrap input tag 
Html :: bootstrap wysiwyg 
Html :: lxml.html get element by id 
Html :: html preview pdf file 
Html :: etiqueta negritahtml 
Html :: bootstrap box 
Html :: table bootstrap dark 
Html :: html tooltip 
Html :: multi item slider html css 
Html :: input datetime without time 
Html :: import ionic html 
Html :: form bootstrap email addres 
Html :: what is html element 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =