Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can a timer be used to execute different javascripts?

Status
Not open for further replies.

mremixer

Technical User
Aug 30, 2007
2
GB
Hi everyone, I'm new to coding so bear with me on this please!

I have 4 javascripts that I want to execute in a timed order.

i.e. on pageload execute myscript1.js
after 30 seconds replace myscript1.js with myscript2.js
after another 30 seconds replace myscript2.js with myscript3.js
again after another 30 seconds replace myscript3.js with myscript4.js
finally reset and replace myscript4.js with myscript1.js and loop round

I have tried a number of permutations of coding that don't work, but it could be I'm doing them wrong! (Highly likely)

I'm looking for other peoples solutions to this problem so I'm not going to embarrass myself by posting my code!

Any help/advice/pointers/examples (preferably with explanations if you don't mind!) would be much appreciated!
 
Alright my embarrassing script follows this is the first way I tried!

<HEAD>
<SCRIPT language="JavaScript">
<!--
function mytimer()
{
var thetime=new Date();

var secs=thetime.getSeconds();
var scriptno=" ";

if (secs>=0 && secs<=14)
scriptno="myscript1.js";
if (secs>=15 && secs<=29)
scriptno="myscript2.js";
if (secs>=30 && secs<=44)
scriptno="myscript3.js";
if (secs>=45 && secs<=59)
scriptno="myscript4.js";

setTimeout('mytimer()',1000);

}

//-->
</SCRIPT>
</HEAD>

<BODY>
<SCRIPT type="text/javascript" src=""+scriptno+""></SCRIPT>
</BODY>

This is the way I'd like to use but if there are better/more efficient/simpler ways of coding I am open to suggestions!

I think my problem is in the "src=""+scriptno+""" but I don't know any other way to call the script! (not even sure if thats the right notation)

It has been suggested to use the following but again I was left confused as to where to place "myscript1.js" etc though I felt this was probably a better method unfortunately I am unable to contact person who suggested this way to find out more!

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

I was also unsure as to where I should place the above (head or body) and whether I need to declare the div?

Well I'm embarrassed enough now, gonna sulk away and read a javascript book, this is and will be (probably) my one & only javascript I want to utilize to call other (pre-made) javascripts to my page, which is why I'm asking for help and didn't feel I needed to know anymore as I (stupidly!) thought it was a very basic thing to achieve!

Thanks One & All For Your Indulgence!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top