setTimeout and entire javascript

Discussion in 'JavaScript' started by Fracisc, Jan 11, 2009.

  1. #1
    Is there any way to delay an entire JavaScript? I mean many functions and stuff in the same time?

    setTimeout(func, 1000);
    
    function func() {
       
    stuff1:function(){
    }
    
    stuff2:function(){
    }
    
    }
    Code (markup):
    I have tried this way but my other functions are not working now...
    So, is there any way to delay everything without messing up the rest of the functions?
     
    Fracisc, Jan 11, 2009 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Cascade them.

    function func1() {
      /* do something */
      setTimeout(func2,1000);
    }
    
    function func2() {
      /* do something else */
      setTimeout(func3,1000);
    }
    
    function func3() {
      /* do a third thing*/
    }
    
    setTimeout(func1,1000);
    Code (markup):
     
    deathshadow, Jan 11, 2009 IP
  3. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #3
    They are to many. Isn't there a way to just delay the loading of a javascipt file?

    <script src="script.js" type="text/javascript"></script> <= load this a few seconds later

    Or just give priority to the loading and do the rest after..
     
    Fracisc, Jan 11, 2009 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    If there are 'too many' you probably have too much javascript for your page... Unless you are designing a full blown web app like google maps, you shouldn't have enough javascript to be worrying about that.

    But I'm the guy who considers more than 15k of javascript (uncompressed, no white space or encoding chicanery) on a website to be a complete and total /FAIL/.

    Just how big is your total script size?
     
    deathshadow, Jan 11, 2009 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    yes you can delay this, something like this:
    
    <script id="myscript" type="text/javascript"></script>
    <script type="text/javascript">
        // onload or domready... 
        window.onload = function() {
            setTimeout(3000, function() {
                document.getElementById("myscript").src = 'script.js'; // load the script.
            });
        }
    </script>
    
    PHP:
    good luck

    p.s. frameworks like mootools often provide methods for chaining, for example, something like:
    $("foobar").fade(0).chain(function() { this.fade(1); }).chain(function() { this.setStyle("border", "1px solid #000"); }); //.. and so forth, google it...
     
    dimitar christoff, Jan 12, 2009 IP