Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Location
    I live on the internet, but exist in that virtual country called ENGLAND!
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Can a timer be used to execute different scripts?

    First of all forgive me if I break the rules & reg's of posting but I find them a little harsh and confusing, i.e. not being allowed to put I'm a newbie and need help! Kinda defeats the object of help forums like this and puts newbies like myself of from actually asking for help!

    Anyway on to my concerns with javascript, I'll try and be as clear and concise as possible, but I am a noob!

    I want to implement a javascript timer to execute 4 or more other javascript files at set periods.

    I.E. at page load = display javascript one
    after 30 seconds = replace javascript one with javascript two
    after 1 minute = replace javascript two with three
    after 1:30 = replace three with four
    after 2:00 = replace four with one & reset timer so cycle starts
    again

    I have a timer and my seperate javascipts (coincidentally called timer.js and javascript1.js - javascript4.js lol).

    I'm not going to insult anybody by posting the scripts as they are bog standard scripts!

    All I want to know is can/should/would/is it possible to have a variable to hold the javascript name (i.e. name="javascript1.js") dependent on the timer (see above) and can I then use the variable name in the the body (div) of my document as the src (i.e. <script> blah de blah src=name blah de blah) to call said javascript into the document?

    I don't want to refresh my page just have different scripts running at different times.

    My apologies if it's not clear & concise enough but I know no other way of explaining my dilemma!

    Thank you all for any help or advice you may be able to offer

  • #2
    Regular Coder 2reikis's Avatar
    Join Date
    Nov 2005
    Location
    New Mexico, USA
    Posts
    174
    Thanks
    18
    Thanked 13 Times in 13 Posts

    setinterval

    You can set an onload handler in your body statement that starts the ball rolling and set up all your timers at once. No need for seperate .js files unless you simply include them.

    Code:
    <body onload="startTimers();">
    should do the trick and then, for a brief tutorial, try http://www.elated.com/articles/javas...d-setinterval/

    or google setinterval and settimeout

    HTH
    2reikis

  • Users who have thanked 2reikis for this post:

    mremixer (09-02-2007)

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Pretty sure setInterval occurs after the page has loaded so using it by itself should also work.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    mremixer (09-02-2007)

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Give this example a try
    Code:
    <script type="text/javascript">
    
    num=4 // number of functions
    
    count=1
    
    function initFunctions(){
    window["myFunction"+count]()
    
    if(count<num){count++}
    else{count=1}
    
    }
    
    timer=setInterval("initFunctions()",30*1000)
    
    function myFunction1(){document.getElementById("d1").innerHTML="Function "+count}
    function myFunction2(){document.getElementById("d1").innerHTML="Function "+count}
    function myFunction3(){document.getElementById("d1").innerHTML="Function "+count}
    function myFunction4(){document.getElementById("d1").innerHTML="Function "+count}
    
    onload=initFunctions
    
    </script>
    
    <div id="d1"></div>
    Last edited by Mr J; 09-02-2007 at 11:19 AM. Reason: put the onload at the end of the script
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • Users who have thanked Mr J for this post:

    mremixer (09-02-2007)

  • #5
    New to the CF scene
    Join Date
    Aug 2007
    Location
    I live on the internet, but exist in that virtual country called ENGLAND!
    Posts
    2
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks one & all will CERTAINLY be trying your suggestions, NEVER thought of setInterval (Doh!).

    To be honest I'm just happy to find out it can be done but am absolutely over the moon with examples to follow .

    Will be back soon to report on outcome, going to play now.

    Right have had a play and come to conclusion I'd like to go with Mr J (seeing as I understand it more!, BUT also coz I have now realized I'm also thicker than I thought! )

    The haze is a little clearer now but I'm still struggling (coz of my total ineptitude not you guys examples and explanations) to get it working.

    I have tried changing things a number of ways for myscripts (I've renamed them for less confusion with the javascript commands/functions etc) but no matter the permutations I'm not getting anything showing on page my changes have been like so:

    replaced myFunction() with myscript() (should this be myscript1.js() or myscript1 or something else?)

    also replaced

    function myfunction1() etc with function myscript1() (again should this be with .js tried both ways but still nothing)

    Sorry for being as inept at this I honestly thought I'd got a grasp on this but obviously I need to learn more!

    One other thing do I need to declare div d1 in my CSS? or can it just be placed straight in as in the example (this is for future reference not exactly important at the moment as I'm still testing I haven't done a CSS yet!)

    More help much appreciated!

    A "thicker than he realised" newbie who is still inept! (I'm kinda glad this is the only bit of javascript I intend on utilising!)
    Last edited by mremixer; 09-03-2007 at 01:29 AM. Reason: Still Got Problems


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •