Search code examples
javascriptjqueryhtmldelaydelayed-execution

Set delay or timeout before initializing


I'm working with a script that needs to delay (or have a setTimeout) before any animation loads or initializes, but can't seem to figure out where to put it.

As for delay, if I'm not mistaken, this is used mainly with jquery...So for example: $('id or class here').delay(2000); ...Correct?

As for the setTimeout, if I'm not mistaken, it would be with javascript correct? If so, wouldn't it look something similar to this: setTimeout(function () {function_name},2000); or a slightly different variation of that?

Regardless of these two approaches and trying to add it where I think it should go (using either variations mentioned above), for some reason it just doesn't work right. The console isn't really helping either to check for errors.

In a nutshell, I'm trying to set a delay of 2s (2000ms) before anything starts or initializes.

JS CODE (Where I believe the issue lies):

$(document).ready(function() {
    // Additional code here...
    // start
    BG.init();

    // Additional code here...
    }
});

Solution

  • Where you have this:

    $(document).ready(function() {
    

    Put this:

    $(document).ready(function() {
        setTimeout(function() {
    

    And then where you have this:

    });
    
    // wrapper for background animation functionality
    var BG = {
    

    Put this:

        }, 2000);
    });
    
    // wrapper for background animation functionality
    var BG = {
    

    And then, if you don't want to incur the wrath of everyone in the world, indent the stuff inside that new function we just created by one more level. 'Cause indentation is the stuff of life.