Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a JavaScript preload() function?

If I check a radio button for the first time I'm getting a short freezing. Checking them a second time everything runs super smooth. I think because they are now in the browser-cache. Any chance of a preload here?

var insideMap1 = THREE.ImageUtils.loadTexture( 'insideMap1.jpg' );
var insideMap2 = THREE.ImageUtils.loadTexture( 'insideMap2.jpg' );
var insideMap3 = THREE.ImageUtils.loadTexture( 'insideMap3.jpg' );

$("input[name='opt1']").change(function() {

    if ($("#radio1").is(":checked")) {
        material[ "inside" ].map = insideMap1;
    }

    if ($("#radio2").is(":checked")) {
       material[ "inside" ].map = insideMap2;
    }

    if ($("#radio3").is(":checked")) {
       material[ "inside" ].map = insideMap3;
    }

});
like image 423
Alexander Hein Avatar asked Dec 23 '15 23:12

Alexander Hein


People also ask

What is the preload function in JavaScript?

Called directly before setup(), the preload() function is used to handle asynchronous loading of external files in a blocking way. If a preload function is defined, setup() will wait until any load calls within have finished.

How do you use preload?

The rel="preload" attribute value is used to preload assets. It can be applied to several file formats, including CSS, JS, fonts, images, and more. Depending on the type of file you would like to preload, the corresponding as attribute may also need to be included along with rel="preload" .

Can you preload JS?

Preloading JavaScript files #Because browsers don't execute preloaded files, preloading is useful to separate fetching from execution which can improve metrics such as Time to Interactive. Preloading works best if you split your JavaScript bundles and only preload critical chunks.

Can I preload jquery?

You can have javascript "preloaded", it loads inline with the rest of the page, and due to it usually being larger than the rest of the page takes longer to download. You can't make javascript load first, that's not the way it works.


1 Answers

Use THREE.Cache:

<script>
    $(document).ready(function() {

        THREE.Cache.enabled = true;

        var url = '/img/DSC03537.JPG';
        var newImg = document.createElement('img');
        newImg.src = url;
        THREE.Cache.add(url, newImg);

        // main program part
        var insideMap2 = THREE.ImageUtils.loadTexture(url);

        $("#testBox").change(function () {
                $("#tstImg").map = insideMap2;
        });
    });
</script>

Notice: THREE.ImageUtils.loadTexture is being deprecated. Use THREE.TextureLoader() instead.

like image 134
Pavel Samoylenko Avatar answered Oct 06 '22 18:10

Pavel Samoylenko