# Downloading images from server in the background, while script is running

**URL:** <https://discourse.processing.org/t/downloading-images-from-server-in-the-background-while-script-is-running/33556>\
**Category:** Beginners\
**Created:** [November 16, 2021, 2:04pm UTC](https://discourse.processing.org/t/downloading-images-from-server-in-the-background-while-script-is-running/33556 "2021-11-16T14:04:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![user101](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/user101/32/15012_2.png) [@user101](https://discourse.processing.org/u/user101)\
**Post date:** [November 16, 2021, 2:04pm UTC](https://discourse.processing.org/t/downloading-images-from-server-in-the-background-while-script-is-running/33556/1 "2021-11-16T14:04:40Z")

</div>

I want to create a website with many, many images on the frontpage. However, not all of these images are visiable from the start of the script on, instead they should be downloaded from the server in the background, while the script is running. How can I do that in p5?

Thanks for any replies!

---

<div class="post-metadata">

**Author:** ![KumuPaul](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kumupaul/32/13072_2.png) [@KumuPaul](https://discourse.processing.org/u/KumuPaul)\
**Post date:** [November 16, 2021, 8:15pm UTC](https://discourse.processing.org/t/downloading-images-from-server-in-the-background-while-script-is-running/33556/2 "2021-11-16T20:15:59Z")

</div>

Definitely. You just need to use the callback function argument(s) of the [`loadImage`](https://p5js.org/reference/#/p5/loadImage) function.

```auto
let loading = false;
let img;

function setup() {
  createCanvas(300, 300);
  textAlign(LEFT, TOP);
}

function draw() {
  if (img) {
    image(img, 0, 0);
  } else if (loading) {
    background('red');
    text("Loading ...", 10, 10);
  } else {
    background(150);
    text("Click to Load", 10, 10);
  }
}

function mousePressed() {
  if (!loading) {
    loading = true;
    loadImage(
      'https://www.paulwheeler.us/files/windows-95-desktop-background.jpg',
      (i) => {
        img = i;
        loading = false;
      },
      () => {
        console.log('failed to load image');
      }
    );
  }
}

```
