View Full Version : Can a timer be used to execute different scripts?

09-02-2007, 01:50 AM
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

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

09-02-2007, 06:53 AM
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.

<body onload="startTimers();">

should do the trick and then, for a brief tutorial, try http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

or google setinterval and settimeout


09-02-2007, 10:06 AM
Pretty sure setInterval occurs after the page has loaded so using it by itself should also work.

Mr J
09-02-2007, 10:55 AM
Give this example a try

<script type="text/javascript">

num=4 // number of functions


function initFunctions(){




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}



<div id="d1"></div>

09-02-2007, 06:56 PM
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 :D :D .

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! :eek: )

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! :thumbsup:

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!)