/    Sign up×
Community /Pin to ProfileBookmark

Two scripts running simultaneously

Hello everyone. I have 2 scripts in which one was actually created using another. Both of the scripts work great individually, however, when I put them in the same page only the one called first runs.

Here is the HTML test page

[code=html]<html>
<head>
<title>TEST</title>

<script language=”javascript” src=”date.js”></script>
<script language=”javascript” src=”time.js”></script>

</head>
<body onload=”date()” onload=”time()”>

<span id=”date”></span><br>
<span id=”time”></span>

</body>
</html>[/code]

Here is date.js

[CODE]function date() {
var mydate = new Date();
var year = mydate.getFullYear();
var day = mydate.getDay();
var month = mydate.getMonth();
var daym = mydate.getDate();
if (daym<10)
daym=”0″+daym;
var dayarray = new Array(“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”);
var montharray = new Array(“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”);
dispDate = “<span class=’datetime’>” + dayarray[day] + “, ” + montharray[month] + ” ” + daym + “, ” + year + “</span>”;
var e = document.getElementById(“date”);
if(!e)return;
if(“innerHTML” in e)e.innerHTML = dispDate;
setTimeout(“date()”, 1000);
}[/CODE]

Here is time.js

[CODE]function time() {
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = “AM”;
if (hours > 11) amOrPm = “PM”;
if (hours > 12) hours = hours – 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = “0” + minutes;
if (seconds <= 9) seconds = “0” + seconds;
dispTime = “<span class=’datetime’>” + hours + “:” + minutes + “:” + seconds + ” ” + amOrPm + “</span>”;
var e = document.getElementById(“time”);
if(!e)return;
if(“innerHTML” in e)e.innerHTML = dispTime;
setTimeout(“time()”, 1000);
}[/CODE]

Can anyone tell me how to make these scripts exist simultaneously? I hope so, because I’ve been trying to get these 2 scripts the way they are now pretty hard for them not to be able to work together ? . Thanks.

lonogod

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@lonogodauthorNov 25.2005 — I'm not an expert but I was thinking that it could be the fact that in each .js file there is a variable with the same value of "new Date". Could that be it? If it is that is there anything else that I can name the value so it will still do the same thing but not conflict? Thanks.

lonogod
Copy linkTweet thisAlerts:
@gphNov 25.2005 — there is only one onload event
<i>
</i>&lt;body onload="date();time()"&gt;
Copy linkTweet thisAlerts:
@lonogodauthorNov 25.2005 — That's fantastic. Thank you so much gph. So should I do that whenever I have 2 or more functions that have to be called? Thanks again gph.

lonogod
Copy linkTweet thisAlerts:
@gphNov 25.2005 — Well this makes for cleaner HTML

<i>
</i>&lt;body&gt;

// then in a js file

onload=function(){
date();
time()
}
Copy linkTweet thisAlerts:
@lonogodauthorNov 25.2005 — Ok. Thanks again gph.

lonogod
×

Success!

Help @lonogod spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...