Delay a Script - Really Nooby Javascript Question

Discussion in 'HTML & Website Design' started by iWrite618, Aug 18, 2009.

  1. #1
    I have a script that I want to delay. I'm a Javascript noob obviously.

    So what I'm asking is how can I get this script to load 15 seconds after the page loads (it's called inside the <head></head> tags):

    <script type="text/javascript" src="lock.js"></script>

    I want to get lock.js to start 15 seconds after the page loads, in short.

    EDIT: I put this in the wrong place, sorry.
     
    iWrite618, Aug 18, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You will have to encapsulate the logic you want to execute in a function.

    Then, call that function like this:

    
    setTimeout(functionName,15000);
    Code (markup):
     
    premiumscripts, Aug 18, 2009 IP
  3. iWrite618

    iWrite618 Guest

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This isn't my code, I bought this (BlackHatCodeBreaker) and it's all encoded, so I have no idea what the function name is, I just need the general script to be executed later, anyway to do that without having to know the functions.

    Maybe a better question to ask is can you write a function that executes a javascript file?
     
    iWrite618, Aug 18, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, you can also try and include the script later like so:

    
    <script type="text/javascript">
    function loadLock() {
        var html_doc = document.getElementsByTagName('head').item(0);
        var js = document.createElement('script');
        js.setAttribute('language', 'javascript');
        js.setAttribute('type', 'text/javascript');
        js.setAttribute('src', 'lock.js');
        html_doc.appendChild(js);
    }
    setTimeout(loadLock,15000);
    </script>
    
    Code (markup):
    However, keep in mind that the file will only be included after 15 seconds, so this is without downloading etc. You may want to change it to 13 or 14 seconds to compensate.
     
    premiumscripts, Aug 18, 2009 IP
  5. iWrite618

    iWrite618 Guest

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hm...didn't work.

    EDIT: I think it's because I have 2 iframes with really long videos in them...it's trying to load them fully before it pops up.
     
    iWrite618, Aug 18, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can just add an alert in the function:

    alert('test');

    See if that pops up after 15 seconds. If it does, everything should be in order and there is a problem with your script.
     
    premiumscripts, Aug 18, 2009 IP
  7. iWrite618

    iWrite618 Guest

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I didn't need to put anything in the body, did I? Just the head tags?
     
    iWrite618, Aug 18, 2009 IP