0

I'd like to initially load a "Caching" function for my Chrome app, then run the actual code, like so:

function preloader(){
    hideDOM();
    loadSpinner();
    for(var i=0;i<x;i++)
        caching (loading resources)...
}

function startAPP(){
    showDOM();
    stopSpinner();
    loadAPP();
}

So what I need is the preloader first, then when the "for" loop ends, load the startAPP.

2
  • 2
    What does the caching step do exactly? Does it load resources? If so, a synchronous loop isn't going to work. Attach onload events to the resources so you'll know when you're done loading. Commented Sep 1, 2015 at 15:59
  • @Halcyon Yes, the caching loads some files to disable latency and delay issues. Could you give me an example for attaching the onload to events please? Commented Sep 1, 2015 at 16:03

1 Answer 1

1

Here is a proof of concept that I cobbled together: (there might be bugs)

function preload_resources(callback) {
    var images = [
        "http://domain/img1.jpg",
        "http://domain/img2.jpg",
        "http://domain/img3.jpg"
    ];

    // count the number of completed load events
    var count = 0;
    function done() {
        count += 1;
        if (count === images.length) {  // all images loaded
            callback();
        }
    }

    // make a hidden div;
    var div = document.createElement("div");
    div.style.display = "none";
    document.body.appendChild(div);

    // add images to DOM
    images.forEach(function (image_url) {
        var img = document.createElement("img");
        img.src = image_url;
        // attach load event
        img.addEventListener("load", done);
        div.appendChild(img);
    });
}

hideDOM();
loadSpinner();
preload_resources(function () {
    showDOM();
    stopSpinner();
    loadAPP();
});

It loads images, in theory you can load anything you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.